Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed Mar 4, 2025
1 parent d7f7e74 commit 6424db9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { SuggestionPlugin } from '@udecode/plate-suggestion/react';

export const skipMarkPlugin = SkipMarkPlugin.configure({
options: {
query: {
allow: [SuggestionPlugin.key, CodePlugin.key, CommentsPlugin.key],
},
allow: [SuggestionPlugin.key, CodePlugin.key, CommentsPlugin.key],
},
});
54 changes: 33 additions & 21 deletions packages/basic-marks/src/lib/BaseSkipMarkPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,65 @@
import {
type PluginConfig,
type QueryNodeOptions,
type Text,
createTSlatePlugin,
queryNode,
RangeApi,
TextApi,
} from '@udecode/plate';

export type SkipMarkConfig = PluginConfig<
'skip-mark',
{
query?: QueryNodeOptions;
allow: string[];
}
>;

export const BaseSkipMarkPlugin = createTSlatePlugin<SkipMarkConfig>({
key: 'skip-mark',
options: {},
options: {
allow: [],
},
}).overrideEditor(({ editor, getOption, tf: { insertText } }) => ({
transforms: {
insertText(text, options) {
if (RangeApi.isExpanded(editor.selection))
return insertText(text, options);

const allow = getOption('allow');

const textNode = editor.api.node<Text>({
mode: 'lowest',
match: (node) => {
if (TextApi.isText(node)) {
return allow.some((key) => !!node[key]);
}
},
});

const query = getOption('query');
if (!textNode) return insertText(text, options);

if (
textNode &&
!queryNode(textNode, query) &&
editor.api.isEnd(editor.selection?.focus, textNode[1])
) {
editor.tf.insertNode({ text });
const _nextPoint = editor.api.start(textNode[1], { next: true });
const nextPoint = editor.api.start(textNode[1], { next: true });

if (!_nextPoint) return;
const nextNode = editor.api.node<Text>({
at: nextPoint,
mode: 'lowest',
match: (node) => {
if (TextApi.isText(node)) {
return allow.some((key) => !!node[key]);
}
},
});

const nextPoint = {
offset: _nextPoint.offset + 1,
path: _nextPoint.path,
};
const isBetweenSameMarks =
nextNode &&
allow.findIndex((key) => !!textNode[0][key]) ===
allow.findIndex((key) => !!nextNode[0][key]);

editor.tf.setSelection({
anchor: nextPoint,
focus: nextPoint,
});
if (
!isBetweenSameMarks &&
editor.api.isEnd(editor.selection?.focus, textNode[1])
) {
editor.tf.removeMarks(allow);
insertText(text, options);

return;
}
Expand Down

0 comments on commit 6424db9

Please sign in to comment.