英文:
GrapesJS trait does not appear after reloading the editor
问题
我有一个React网页应用程序(使用TypeScript,如果有关系)我正在尝试将GrapesJS HTML编辑器集成到其中。
它呈现在一个功能组件中,该组件会随着外部依赖项的更新而更新,从而刷新编辑器。
在其中定义了一个block,并定义了一个trait,当添加或选择按钮时,该trait会出现在编辑器的设置面板中,并且它也作为项目数据的一部分保存了下来。
然而,出于任何原因重新加载编辑器后,设置面板将不再显示该trait,而是恢复为默认的id, title
traits:
这是组件代码:
import React, { useEffect, useRef } from 'react';
import grapesjs, { Editor } from 'grapesjs';
import store, { StoreActions } from '../../state/store';
import { useCurrentPage } from '../ReactUtils';
import { useTypedDispatch } from '../../state/hooks';
export default function GrapesEditorWindow(props: any)
{
// ...(中文代码省略)...
return <div id={"gjs"} ref={grapesEditorContainerRef} />;
}
这是存储数据(请注意,组件具有名为option-value
的属性,其值已设置):
//...
components: [
{
tagName: 'button',
name: 'Button',
content: 'Click Me',
attributes: {
id: 'ija4',
'option-value': '2'
}
}
]
//...
我尝试找出可能的问题所在,并遇到了这个帖子:https://github.com/GrapesJS/grapesjs/issues/2106 我尝试以相同的方式实现trait,使用TraitManager,但结果相同(重新加载编辑器后trait不显示):
//...
blocks: [
{
label: 'Button',
content: {
tagName: 'button',
name: "Button",
content: "Click Me",
traits: [
{
label: 'Value',
name: 'option-value',
type: 'my-option-value',
}
],
},
},
],
//...
editor.TraitManager.addType('my-option-value', {
createInput({ trait })
{
const el = document.createElement('select');
el.innerHTML = `
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>`;
return el;
}
});
//...
我还遇到了这个帖子,它建议将块编写为插件:https://github.com/GrapesJS/grapesjs/issues/2136 但这也导致了相同的结果(重新加载编辑器后trait不显示):
// 不同的文件
import { Plugin } from 'grapesjs';
export const gjsplugin: Plugin<any> = (editor, opts = {}) =>
{
const blockManager = editor.BlockManager;
blockManager.add('my-button',
{
label: 'My Button',
content: {
type: 'button',
tagName: 'button',
traits: [
{
label: 'Value',
name: 'option-value',
type: 'select',
options: [
{ value: '1' },
{ value: '2' },
{ value: '3' },
],
}
],
}
});
}
export default gjsplugin;
// 主文件
//...
import gjsplugin from './gjsplugin';
//...
const editor = grapesjs.init({
container: containerElement,
storageManager: false,
blockManager: {
appendOnClick: true,
},
plugins: [
gjsplugin,
],
deviceManager: {
devices: [
{
name: 'Desktop',
width: '1920',
height: '1200',
},
],
},
});
//...
到目前为止,我尝试的一切都没有奏效,重新加载后trait从未显示在编辑器中。
我一定是做错了些什么,因为traits对于内部块类型(如video
)或导入现有插件(如GrapesJS Forms)有效,但我无法弄清楚到底是什么问题。
英文:
I have a React web app (using TypeScript, if it matters) that I'm trying to integrate GrapesJS html editor into.
It's rendered in a functional component that gets updated with external dependencies, refreshing the editor.
There is a defined block with a defined trait and that trait appears in the editor's Settings panel when adding or selecting the button, and it's also saved as part of the project data.
However, when reloading the editor for any reason, the Settings panel no longer displays the trait, instead reverting back to the default id, title
traits:
This is the component code:
import React, { useEffect, useRef } from 'react';
import grapesjs, { Editor } from 'grapesjs';
import store, { StoreActions } from '../../state/store';
import { useCurrentPage } from '../ReactUtils';
import { useTypedDispatch } from '../../state/hooks';
export default function GrapesEditorWindow(props: any)
{
const currentPage = useCurrentPage(); // this hook causes the component to re-render
const dispatch = useTypedDispatch();
const grapesEditorContainerRef = useRef<HTMLDivElement>(null);
const grapesEditor = useRef<Editor>();
useEffect(() =>
{
return () => grapesEditor.current?.destroy();
}, []);
useEffect(() =>
{
if (grapesEditorContainerRef.current)
{
grapesEditor.current = createGrapesEditor(grapesEditorContainerRef.current);
grapesEditor.current.loadProjectData(store.getState().gjsModule.pageUIData[currentPage.id]);
}
}, [grapesEditorContainerRef.current, currentPage]);
function createGrapesEditor(containerElement: HTMLDivElement): Editor
{
const editor = grapesjs.init({
container: containerElement,
storageManager: false,
blockManager: {
appendOnClick: true,
blocks: [
{
label: 'Button',
content: {
tagName: 'button',
name: "Button",
content: "Click Me",
traits: [
{
label: 'Value',
name: 'option-value',
type: 'select',
options: [
{ value: "1" },
{ value: "2" },
{ value: "3" },
],
}
],
},
},
],
},
deviceManager: {
devices: [
{
name: 'Desktop',
width: '1920',
height: '1200',
},
],
},
});
editor.on('update', function()
{
const data = grapesEditor.current?.getProjectData();
dispatch(StoreActions.gjs.setPageUIData({ pageId: currentPage.id, data: data }));
});
return editor;
}
return <div id={"gjs"} ref={grapesEditorContainerRef} />;
}
And this is the store data (note that the component has an attribute named option-value
and its value is set):
//...
components: [
{
tagName: 'button',
name: 'Button',
content: 'Click Me',
attributes: {
id: 'ija4',
'option-value': '2'
}
}
]
//...
I've tried to find out what could be the problem, and encountered this thread: https://github.com/GrapesJS/grapesjs/issues/2106 I've tried to implement the trait in the same way, using TraitManager, with the same result (trait does not show after editor reload):
//...
blocks: [
{
label: 'Button',
content: {
tagName: 'button',
name: "Button",
content: "Click Me",
traits: [
{
label: 'Value',
name: 'option-value',
type: 'my-option-value',
}
],
},
},
],
//...
editor.TraitManager.addType('my-option-value', {
createInput({ trait })
{
const el = document.createElement('select');
el.innerHTML = `
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>`;
return el;
}
});
//...
I've also come across this thread, which suggest writing the block as a plugin instead: https://github.com/GrapesJS/grapesjs/issues/2136
But that too has the same result (trait not showing after editor reload):
// different file
import { Plugin } from 'grapesjs';
export const gjsplugin: Plugin<any> = (editor, opts = {}) =>
{
const blockManager = editor.BlockManager;
blockManager.add('my-button',
{
label: 'My Button',
content: {
type: 'button',
tagName: 'button',
traits: [
{
label: 'Value',
name: 'option-value',
type: 'select',
options: [
{ value: '1' },
{ value: '2' },
{ value: '3' },
],
}
],
}
});
}
export default gjsplugin;
// main file
//...
import gjsplugin from './gjsplugin';
//...
const editor = grapesjs.init({
container: containerElement,
storageManager: false,
blockManager: {
appendOnClick: true,
},
plugins: [
gjsplugin,
],
deviceManager: {
devices: [
{
name: 'Desktop',
width: '1920',
height: '1200',
},
],
},
});
//...
So far, nothing I've tried worked, and the traits never show in the editor after a reload.
I must be doing something wrong as traits work for internal block types like video
or when importing existing plugins such as GrapesJS Forms, but I can't figure out what exactly.
答案1
得分: 1
在我的情况下,当我使用Vue和Laravel实现从数据库保存/恢复数据时,我遇到了相同的问题。
我看到你尝试将它制作成插件,对我来说有效,我唯一看到你的代码和我的代码之间的区别是你添加类型的位置:
editor.TraitManager.addType('my-option-value', {
createInput({ trait }) {
const el = document.createElement('select');
el.innerHTML = `
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>`;
return el;
}
});
相反,我将类型添加到DomComponents,所以你的代码应该是:
function pluginName(editor){
editor.DomComponents.addType(...your code)
}
然后在初始化GrapesJS时在你的插件列表中使用它。
希望它能起作用!
英文:
in my case I was having the same problem when implementing it with Vue and Laravel saving/restoring data from the database.
I see you tried to make it a plugin, that worked for me, only difference I can see between your code and mine is where you add the type
editor.TraitManager.addType('my-option-value', {
createInput({ trait })
{
const el = document.createElement('select');
el.innerHTML = `
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>`;
return el;
}
});
Instead, I add the type in DomComponents, so your code would be
function pluginName(editor){
editor.DomComponents.addType(...your code)
}
And then in your plugins list when initializing GrapesJS.
Hope it works!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论