葡萄JS 反应
要求葡萄JS v0.21.3 或更高
官员 葡萄JS( 葡萄) react 的包装,允许您为编辑器构建自定义和声明UI.
这个库的目的不是提供UI组件,而是在核心葡萄JS模块周围提供简单的包装,让你定义自己的UI组件并方便地访问GrapesJS API.
3⁄4 ̄ ̧漯B: 这个库并不是为了在 GrapesJS 画布中渲染出您的反应组件, 这里所有的内容都是在画布本身周围的自定义 UI .
安装
核心内容 grapesjs 库不与包件本身相捆绑,所以必须分别安装.
npm i grapesjs @grapesjs/react
兼容性
@grapesjs/反应 v1.X
- 反应 v18.X
- 葡萄JS v0.21.3或以上
- 下一个JS v14.X
@grapesjs/反应 v2.X
- 反应 v18.X 或 v19.X
- 葡萄JS v0.22.5 或更高
- 下一个JS v15.X
默认 UI
这是使用GrapesJS包装最简单更传统的方法,因为它依赖于GrapesJS提供的默认UI. 从React的角度来看,这种方法不太适合自定义,所有的UI定制都必须通过GrapesJS(基本上没有包装器你会怎么做)来完成.
import grapesjs, { Editor } from 'grapesjs';
import GjsEditor from '@grapesjs/react';
export default function DefaultEditor() {
const onEditor = (editor: Editor) => {
console.log('Editor loaded', { editor });
};
return (
<GjsEditor
// Pass the core GrapesJS library to the wrapper (required).
// You can also pass the CDN url (eg. "https://unpkg.com/grapesjs")
grapesjs={grapesjs}
// Load the GrapesJS CSS file asynchronously from URL.
// This is an optional prop, you can always import the CSS directly in your JS if you wish.
grapesjsCss="https://unpkg.com/grapesjs/dist/css/grapes.min.css"
// GrapesJS init options
options={{
height: '100vh',
storageManager: false,
}}
onEditor={onEditor}
/>
);
}
自定义用户界面
这是使用包装器的更有力和宣示性的方法,因为它让你完全控制了编辑的UI. 使用 <Canvas/> 主包件中的组件会禁用核心默认的 UI .
import grapesjs, { Editor } from 'grapesjs';
import GjsEditor, { Canvas } from '@grapesjs/react';
export default function CustomEditor() {
const onEditor = (editor: Editor) => {
console.log('Editor loaded', { editor });
};
return (
<GjsEditor
grapesjs={grapesjs}
grapesjsCss="https://unpkg.com/grapesjs/dist/css/grapes.min.css"
onEditor={onEditor}
options={{
height: '100vh',
storageManager: false,
}}
>
<div>
// ... use your UI components
<Canvas /> // place the GrapesJS canvas where you wish
// ...
</div>
</GjsEditor>
);
}
您可以在下面找到一个很好的例子( 使用所有可用的提供者) , 通过使用自己的 React 组件, 您可以在 GrapesJS 周围构建一个完整的自定义编辑器 .
说明:应用程序没有覆盖所有 葡萄JS API 但只是提供了一个很好的起点 来理解如何创建自己的自定义编辑器.
把编辑器放进组件
您可以轻松地访问自定义组件中的编辑器实例 useEditor 钩. 唯一的警告是,一旦编辑器实际创建出来,就使用它 <WithEditor> 作为包装的组件。 或者,你可以使用 useEditorMaybe 钩并亲自检查编辑器是否存在.
import GjsEditor, { WithEditor, useEditor, useEditorMaybe } from '@grapesjs/react';
function MyComponentWithUseEditor() {
// The `editor` is always defined.
const editor = useEditor();
return (...);
}
function MyComponentWithUseEditorMaybe() {
// The `editor` is not immediately available
const editor = useEditorMaybe();
return (
<div>
<div>I will be rendered immediately</div>
<div>
Editor: { editor ? 'created' : 'not yet created' }.
</div>
</div>
);
}
export default function CustomEditor() {
return (
<GjsEditor ...>
// ...
<WithEditor>
// This component won't be rendered until the editor is created
<MyComponentWithUseEditor/>
</WithEditor>
<MyComponentWithUseEditorMaybe/>
</GjsEditor>
);
}
通过使用内置提供者内部的组件
editor总是被定义.
发展
清除仓库
$ git clone https://github.com/GrapesJS/react.git
$ cd react
安装它
$ yarn i
启动 dev 服务器
$ yarn start
许可证
麻省理工学院