Skip to content

Commit ad74709

Browse files
committed
fix: cover ellipsis rule in code
1 parent 2bbc465 commit ad74709

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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: {markType: codeMarkName},
19+
});
20+
21+
describe('BaseInputRules ellipsis', () => {
22+
it('does not replace triple dots inside inline code', () => {
23+
const startDoc = doc(p(c('text<a>')));
24+
const state = EditorState.create({
25+
schema,
26+
doc: startDoc,
27+
selection: TextSelection.create(startDoc, startDoc.tag.a),
28+
plugins,
29+
});
30+
const view = new EditorView(document.createElement('div'), {state});
31+
32+
const handled = view.someProp('handleTextInput', (f: any) =>
33+
f(view, startDoc.tag.a, startDoc.tag.a, '...'),
34+
);
35+
36+
if (!handled) {
37+
view.dispatch(view.state.tr.insertText('...', startDoc.tag.a, startDoc.tag.a));
38+
}
39+
40+
expect(view.state.doc).toMatchNode(doc(p(c('text...'))));
41+
});
42+
43+
it('replaces triple dots in regular text', () => {
44+
const startDoc = doc(p('text<a>'));
45+
const state = EditorState.create({
46+
schema,
47+
doc: startDoc,
48+
selection: TextSelection.create(startDoc, startDoc.tag.a),
49+
plugins,
50+
});
51+
const view = new EditorView(document.createElement('div'), {state});
52+
53+
const handled = view.someProp('handleTextInput', (f: any) =>
54+
f(view, startDoc.tag.a, startDoc.tag.a, '...'),
55+
);
56+
57+
if (!handled) {
58+
view.dispatch(view.state.tr.insertText('...', startDoc.tag.a, startDoc.tag.a));
59+
}
60+
61+
expect(view.state.doc).toMatchNode(doc(p('text…')));
62+
});
63+
});
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
import {ellipsis} from 'prosemirror-inputrules';
1+
import {InputRule} from 'prosemirror-inputrules';
22

33
import type {ExtensionAuto} from '../../../core';
4+
import {hasCodeMark} from '../../../utils/inputrules';
5+
6+
const ellipsisInputRule = new InputRule(/\.\.\.$/, (state, match, start, end) => {
7+
if (hasCodeMark(state, match, start, end)) return null;
8+
return state.tr.insertText('…', start, end);
9+
});
410

511
export const BaseInputRules: ExtensionAuto = (builder) => {
6-
builder.addInputRules(() => ({rules: [ellipsis]}));
12+
builder.addInputRules(() => ({rules: [ellipsisInputRule]}));
713
};

0 commit comments

Comments
 (0)