|
| 1 | +/** @jsx jsxt */ |
| 2 | + |
| 3 | +import { |
| 4 | + acceptSuggestion, |
| 5 | + BaseSuggestionPlugin, |
| 6 | + getSuggestionKey, |
| 7 | + rejectSuggestion, |
| 8 | +} from '@platejs/suggestion'; |
| 9 | +import { jsxt } from '@platejs/test-utils'; |
| 10 | +import type { SlateEditor } from 'platejs'; |
| 11 | +import { createSlateEditor } from 'platejs'; |
| 12 | + |
| 13 | +import { BaseEditorKit } from '@/registry/components/editor/editor-base-kit'; |
| 14 | + |
| 15 | +jsxt; |
| 16 | + |
| 17 | +const createEditor = (input: SlateEditor) => |
| 18 | + createSlateEditor({ |
| 19 | + plugins: BaseEditorKit, |
| 20 | + selection: input.selection, |
| 21 | + value: input.children, |
| 22 | + } as any); |
| 23 | + |
| 24 | +describe('suggestion link integration', () => { |
| 25 | + it('marks only the previous link character when deleting backward after a link', () => { |
| 26 | + const input = ( |
| 27 | + <editor> |
| 28 | + <hp> |
| 29 | + <htext>before </htext> |
| 30 | + <ha url="https://example.com">link</ha> |
| 31 | + <htext> |
| 32 | + <cursor /> |
| 33 | + {' after'} |
| 34 | + </htext> |
| 35 | + </hp> |
| 36 | + </editor> |
| 37 | + ) as any as SlateEditor; |
| 38 | + |
| 39 | + const output = ( |
| 40 | + <editor> |
| 41 | + <hp> |
| 42 | + <htext>before </htext> |
| 43 | + <ha url="https://example.com"> |
| 44 | + <htext>lin</htext> |
| 45 | + <htext |
| 46 | + suggestion |
| 47 | + suggestion_1={{ |
| 48 | + id: 'placeholder', |
| 49 | + createdAt: 0, |
| 50 | + type: 'remove', |
| 51 | + userId: 'alice', |
| 52 | + }} |
| 53 | + > |
| 54 | + <cursor />k |
| 55 | + </htext> |
| 56 | + </ha> |
| 57 | + <htext>{' after'}</htext> |
| 58 | + </hp> |
| 59 | + </editor> |
| 60 | + ) as any as SlateEditor; |
| 61 | + |
| 62 | + const editor = createEditor(input); |
| 63 | + editor.setOption(BaseSuggestionPlugin, 'isSuggesting', true); |
| 64 | + |
| 65 | + editor.tf.deleteBackward(); |
| 66 | + |
| 67 | + const outputLinkNode = output.children[0].children[1] as any; |
| 68 | + const linkNode = editor.children[0].children[1] as any; |
| 69 | + const suggestionLeaf = linkNode.children[1] as any; |
| 70 | + const suggestionData = editor |
| 71 | + .getApi(BaseSuggestionPlugin) |
| 72 | + .suggestion.suggestionData(suggestionLeaf) as any; |
| 73 | + |
| 74 | + expect(editor.children[0].children[0]).toEqual( |
| 75 | + output.children[0].children[0] |
| 76 | + ); |
| 77 | + expect(linkNode.children[0]).toEqual(outputLinkNode.children[0]); |
| 78 | + expect(suggestionLeaf.text).toBe(outputLinkNode.children[1].text); |
| 79 | + expect(suggestionData?.type).toBe('remove'); |
| 80 | + expect(suggestionData?.userId).toBe('alice'); |
| 81 | + expect(linkNode.suggestion).toBeUndefined(); |
| 82 | + expect( |
| 83 | + Object.keys(linkNode).filter((key) => key.startsWith('suggestion_')) |
| 84 | + ).toHaveLength(0); |
| 85 | + expect(editor.children[0].children[2]).toEqual( |
| 86 | + output.children[0].children[2] |
| 87 | + ); |
| 88 | + expect(editor.selection).toEqual(output.selection); |
| 89 | + }); |
| 90 | + |
| 91 | + it('removes an empty link after accepting the last removed character', () => { |
| 92 | + const removeData = { |
| 93 | + id: '1', |
| 94 | + createdAt: Date.now(), |
| 95 | + type: 'remove', |
| 96 | + userId: 'alice', |
| 97 | + }; |
| 98 | + |
| 99 | + const input = ( |
| 100 | + <editor> |
| 101 | + <hp> |
| 102 | + before{' '} |
| 103 | + <ha url="https://reactjs.org"> |
| 104 | + <htext suggestion_1={removeData} suggestion> |
| 105 | + t |
| 106 | + </htext> |
| 107 | + </ha> |
| 108 | + </hp> |
| 109 | + </editor> |
| 110 | + ) as any as SlateEditor; |
| 111 | + |
| 112 | + const output = ( |
| 113 | + <editor> |
| 114 | + <hp>before </hp> |
| 115 | + </editor> |
| 116 | + ) as any as SlateEditor; |
| 117 | + |
| 118 | + const editor = createEditor(input); |
| 119 | + editor.selection = { |
| 120 | + anchor: { offset: 1, path: [0, 1, 0] }, |
| 121 | + focus: { offset: 1, path: [0, 1, 0] }, |
| 122 | + }; |
| 123 | + |
| 124 | + acceptSuggestion(editor, { |
| 125 | + keyId: getSuggestionKey('1'), |
| 126 | + suggestionId: '1', |
| 127 | + } as any); |
| 128 | + |
| 129 | + expect(editor.children).toEqual(output.children); |
| 130 | + }); |
| 131 | + |
| 132 | + it('rejects remove suggestion on inline link elements', () => { |
| 133 | + const removeData = { |
| 134 | + id: '1', |
| 135 | + createdAt: Date.now(), |
| 136 | + type: 'remove', |
| 137 | + userId: 'alice', |
| 138 | + }; |
| 139 | + |
| 140 | + const input = ( |
| 141 | + <editor> |
| 142 | + <hp> |
| 143 | + before{' '} |
| 144 | + <ha suggestion suggestion_1={removeData} url="https://example.com"> |
| 145 | + link |
| 146 | + </ha>{' '} |
| 147 | + after |
| 148 | + </hp> |
| 149 | + </editor> |
| 150 | + ) as any as SlateEditor; |
| 151 | + |
| 152 | + const output = ( |
| 153 | + <editor> |
| 154 | + <hp> |
| 155 | + before <ha url="https://example.com">link</ha> after |
| 156 | + </hp> |
| 157 | + </editor> |
| 158 | + ) as any as SlateEditor; |
| 159 | + |
| 160 | + const editor = createEditor(input); |
| 161 | + |
| 162 | + rejectSuggestion(editor, { |
| 163 | + keyId: 'suggestion_1', |
| 164 | + suggestionId: '1', |
| 165 | + } as any); |
| 166 | + |
| 167 | + expect(editor.children).toEqual(output.children); |
| 168 | + }); |
| 169 | +}); |
0 commit comments