|
| 1 | +import {EditorState, TextSelection} from 'prosemirror-state'; |
| 2 | +import {builders} from 'prosemirror-test-builder'; |
| 3 | +import {EditorView} from 'prosemirror-view'; |
| 4 | + |
| 5 | +import {ExtensionsManager} from '../../../core'; |
| 6 | +import {CodeSpecs, codeMarkName} from '../../markdown/Code/CodeSpecs'; |
| 7 | +import {BaseNode, BaseSchemaSpecs} from '../specs'; |
| 8 | + |
| 9 | +import {BaseInputRules} from './index'; |
| 10 | + |
| 11 | +const {schema, plugins} = new ExtensionsManager({ |
| 12 | + extensions: (builder) => builder.use(BaseSchemaSpecs, {}).use(CodeSpecs).use(BaseInputRules), |
| 13 | +}).build(); |
| 14 | + |
| 15 | +const {doc, p, c} = builders<'doc' | 'p', 'c'>(schema, { |
| 16 | + doc: {nodeType: BaseNode.Doc}, |
| 17 | + p: {nodeType: BaseNode.Paragraph}, |
| 18 | + c: {nodeType: codeMarkName}, |
| 19 | +}); |
| 20 | + |
| 21 | +describe('BaseInputRules', () => { |
| 22 | + it('replaces triple dots with ellipsis outside code marks', () => { |
| 23 | + const startDoc = doc(p('')); |
| 24 | + const state = EditorState.create({ |
| 25 | + schema, |
| 26 | + doc: startDoc, |
| 27 | + selection: TextSelection.create(startDoc, 1), |
| 28 | + plugins, |
| 29 | + }); |
| 30 | + const view = new EditorView(document.createElement('div'), {state}); |
| 31 | + const from = view.state.selection.from; |
| 32 | + let handled = false; |
| 33 | + view.someProp('handleTextInput', (f) => { |
| 34 | + handled = f(view, from, from, '...'); |
| 35 | + }); |
| 36 | + if (!handled) { |
| 37 | + view.dispatch(view.state.tr.insertText('...', from, from)); |
| 38 | + } |
| 39 | + expect(view.state.doc).toMatchNode(doc(p('…'))); |
| 40 | + view.destroy(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('does not replace triple dots inside inline code', () => { |
| 44 | + const startDoc = doc(p(c('foo'))); |
| 45 | + const state = EditorState.create({ |
| 46 | + schema, |
| 47 | + doc: startDoc, |
| 48 | + selection: TextSelection.create(startDoc, 4), |
| 49 | + plugins, |
| 50 | + }); |
| 51 | + const view = new EditorView(document.createElement('div'), {state}); |
| 52 | + const from = view.state.selection.from; |
| 53 | + let handled = false; |
| 54 | + view.someProp('handleTextInput', (f) => { |
| 55 | + handled = f(view, from, from, '...'); |
| 56 | + }); |
| 57 | + if (!handled) { |
| 58 | + view.dispatch(view.state.tr.insertText('...', from, from)); |
| 59 | + } |
| 60 | + expect(view.state.doc).toMatchNode(doc(p(c('foo...')))); |
| 61 | + view.destroy(); |
| 62 | + }); |
| 63 | +}); |
0 commit comments