我有一个Prosemirror编辑器文本,我创建了一个插件,应用一些装饰文本。它一直工作得很好,但是,当我试图编辑文本,它看起来真的有问题,样式没有正确应用到文本被编辑。
文本原本是橙子背景,但当我开始编辑带有装饰的文本时,装饰从开始更新的那一刻起就消失了或只显示在某些部分
下面的视频演示了该问题:
https://imgur.com/RCETIoO
基本上,这是生成装饰的代码:
export const getDecorationsForAnnotations = (
doc: any,
data: Data[],
selectedDataId?: string
) => {
let decos: any[] = [];
let initialPos: number | undefined = undefined;
let finalPos: number | undefined = undefined;
doc.descendants((node: Node, pos: number, parent: any) => {
...
// ... my logic to filter nodes here
decos.push(
Decoration.inline(pos, pos + parent.content.size, {
style: S.DECORATED_PROSEMIRROR_ANNOTATED_SELECTED_NODE,
})
);
}
}
}
return true;
});
return { decos };
};
export const getHighlightAnnotationsPlugin = (
...
) => {
return new Plugin({
key: new PluginKey("pluginHighlightNotes"),
state: {
init(config, editorState) {
const doc = editorState.doc as any;
return DecorationSet.create(doc, []);
},
apply(transaction, oldEditorState, newEditorState) {
let data: Data[] | undefined = undefined;
let decorationSet = undefined;
let selectedDataId = undefined;
const mark = new Mark(); // being used to pass metadata to pluginView ( to add components in the DOM )
if (transaction.getMeta("isTransactionToListen")) {
data = transaction.getMeta("data") as Data[];
selectedDataId = transaction.getMeta("selectedDataId") as string;
} else {
if (!data && oldEditorState instanceof DecorationSet) {
// reuse previous state and decorations
decorationSet = oldEditorState;
}
}
if (!decorationSet && data?.length) {
const doc = transaction.doc;
const { decos } = getDecorationsForAnnotations(
doc,
data,
selectedDataId
);
decorationSet = DecorationSet.create(doc, decos);
}
return decorationSet ? decorationSet : DecorationSet.create(transaction.doc, []);
},
},
view: (editorView: any) => {
return new pluginView(...);
},
props: {
decorations(state) {
return this.getState(state);
},
},
});
};
1条答案
按热度按时间mrfwxfqh1#
我可以这样做来解决这个问题:
1.在插件视图中:
提取鼠标光标位置(最后状态.选择.范围[0])
1.检查光标位置是否在修饰文本范围内
1.如果光标位于装饰文本范围内,则触发新的Prosemirror事务以刷新文本上的装饰