Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed Mar 4, 2025
1 parent 6424db9 commit 4b876da
Show file tree
Hide file tree
Showing 2 changed files with 177 additions and 9 deletions.
166 changes: 166 additions & 0 deletions packages/basic-marks/src/lib/BaseSkipMarkPlugin.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/** @jsx jsxt */

import type { SlateEditor } from '@udecode/plate';

import { BaseParagraphPlugin, createSlateEditor } from '@udecode/plate';
import { jsxt } from '@udecode/plate-test-utils';

import { BaseBoldPlugin } from './BaseBoldPlugin';
import { BaseCodePlugin } from './BaseCodePlugin';
import { BaseItalicPlugin } from './BaseItalicPlugin';
import { BaseSkipMarkPlugin } from './BaseSkipMarkPlugin';

jsxt;

const skipMarkPlugin = BaseSkipMarkPlugin.configure({
options: {
allow: [BaseCodePlugin.key],
},
});
const plugins = [
BaseParagraphPlugin,
BaseBoldPlugin,
BaseCodePlugin,
BaseItalicPlugin,
skipMarkPlugin,
];

describe('BaseSkipMarkPlugin', () => {
describe('insertText', () => {
describe('when cursor is not in a mark', () => {
it('should insert text normally', () => {
const input = (
<editor>
<hp>
test
<cursor />
</hp>
</editor>
) as any as SlateEditor;

const output = (
<editor>
<hp>
testinserted
<cursor />
</hp>
</editor>
) as any as SlateEditor;

const editor = createSlateEditor({
plugins,
selection: input.selection,
value: input.children,
});

editor.tf.insertText('inserted');

expect(editor.children).toEqual(output.children);
});
});

describe('when cursor is at the end of a mark', () => {
it('should remove marks when inserting text', () => {
const input = (
<editor>
<hp>
<htext code>test</htext>
<cursor />
</hp>
</editor>
) as any as SlateEditor;

const output = (
<editor>
<hp>
<htext code>test</htext>
<htext>inserted</htext>
<cursor />
</hp>
</editor>
) as any as SlateEditor;

const editor = createSlateEditor({
plugins,
selection: input.selection,
value: input.children,
});

editor.tf.insertText('inserted');

expect(editor.children).toEqual(output.children);
});

it('should not remove marks when between same marks', () => {
const input = (
<editor>
<hp>
<htext code>test</htext>
<cursor />
<htext bold code>
more
</htext>
</hp>
</editor>
) as any as SlateEditor;

const output = (
<editor>
<hp>
<htext code>testinserted</htext>
<cursor />
<htext bold code>
more
</htext>
</hp>
</editor>
) as any as SlateEditor;

const editor = createSlateEditor({
plugins,
selection: input.selection,
value: input.children,
});

editor.tf.insertText('inserted');

expect(editor.children).toEqual(output.children);
});
});

describe('when different mark types are adjacent', () => {
it('should remove marks when inserting text', () => {
const input = (
<editor>
<hp>
<htext code>test</htext>
<cursor />
<htext italic>more</htext>
</hp>
</editor>
) as any as SlateEditor;

const output = (
<editor>
<hp>
<htext code>test</htext>
<htext>inserted</htext>
<cursor />
<htext italic>more</htext>
</hp>
</editor>
) as any as SlateEditor;

const editor = createSlateEditor({
plugins,
selection: input.selection,
value: input.children,
});

editor.tf.insertText('inserted');

expect(editor.children).toEqual(output.children);
});
});
});
});
20 changes: 11 additions & 9 deletions packages/basic-marks/src/lib/BaseSkipMarkPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ export const BaseSkipMarkPlugin = createTSlatePlugin<SkipMarkConfig>({

const nextPoint = editor.api.start(textNode[1], { next: true });

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

const isBetweenSameMarks =
nextNode &&
Expand Down

0 comments on commit 4b876da

Please sign in to comment.