|
| 1 | +import {Node} from 'prosemirror-model'; |
| 2 | +import {EditorState, TextSelection, Transaction, Selection, Command} from 'prosemirror-state'; |
| 3 | +import {builders} from 'prosemirror-test-builder'; |
| 4 | + |
| 5 | +import {ExtensionsManager} from '../core'; |
| 6 | +import {get$Cursor, isNodeSelection} from '../utils/selection'; |
| 7 | +import {BaseNode, BaseSpecsPreset} from '../extensions/base/specs'; |
| 8 | +import { |
| 9 | + blockquoteNodeName, |
| 10 | + BlockquoteSpecs, |
| 11 | + isBlockqouteNode, |
| 12 | + DeflistNode, |
| 13 | + DeflistSpecs, |
| 14 | + HtmlAttr, |
| 15 | + HtmlNode, |
| 16 | + Html, |
| 17 | +} from '../extensions/markdown/specs'; |
| 18 | + |
| 19 | +import {joinPreviousBlock} from './join'; |
| 20 | + |
| 21 | +const {schema} = new ExtensionsManager({ |
| 22 | + extensions: (builder) => |
| 23 | + builder.use(BaseSpecsPreset, {}).use(BlockquoteSpecs).use(Html).use(DeflistSpecs, {}), |
| 24 | +}).buildDeps(); |
| 25 | + |
| 26 | +const {doc, p, bq, htmlBlock, dList, dTerm, dDesc} = builders(schema, { |
| 27 | + doc: {nodeType: BaseNode.Doc}, |
| 28 | + p: {nodeType: BaseNode.Paragraph}, |
| 29 | + bq: {nodeType: blockquoteNodeName}, |
| 30 | + htmlBlock: {nodeType: HtmlNode.Block}, |
| 31 | + dList: {nodeType: DeflistNode.List}, |
| 32 | + dTerm: {nodeType: DeflistNode.Term}, |
| 33 | + dDesc: {nodeType: DeflistNode.Desc}, |
| 34 | +}) as PMTestBuilderResult<'doc' | 'p' | 'bq' | 'htmlBlock', 'dList' | 'dTerm' | 'dDesc'>; |
| 35 | + |
| 36 | +function shouldDispatch( |
| 37 | + cmd: Command, |
| 38 | + init: {doc: Node; sel?: (doc: Node) => Selection}, |
| 39 | + expected: {doc: Node; sel?: (sel: Selection) => boolean}, |
| 40 | + onSuccess?: (tr: Transaction) => void, |
| 41 | +) { |
| 42 | + const state = EditorState.create({ |
| 43 | + doc: init.doc, |
| 44 | + selection: init.sel?.(init.doc), |
| 45 | + }); |
| 46 | + |
| 47 | + let tr: Transaction; |
| 48 | + const res = cmd(state, (tr_) => { |
| 49 | + tr = tr_; |
| 50 | + }); |
| 51 | + |
| 52 | + expect(res).toBe(true); |
| 53 | + expect(tr!.doc).toMatchNode(expected.doc); |
| 54 | + if (expected.sel) expect(expected.sel(tr!.selection)).toBe(true); |
| 55 | + |
| 56 | + onSuccess?.(tr!); |
| 57 | +} |
| 58 | + |
| 59 | +describe('joinPreviousBlock', () => { |
| 60 | + const joinPrevQuote = joinPreviousBlock({ |
| 61 | + checkPrevNode: isBlockqouteNode, |
| 62 | + skipNode: isBlockqouteNode, |
| 63 | + }); |
| 64 | + |
| 65 | + const shouldDispatchWithCursorSelection = ( |
| 66 | + initDoc: Node, |
| 67 | + initCursorSel: number, |
| 68 | + expectDoc: Node, |
| 69 | + expectCursor: number, |
| 70 | + ) => |
| 71 | + shouldDispatch( |
| 72 | + joinPrevQuote, |
| 73 | + {doc: initDoc, sel: (doc) => TextSelection.create(doc, initCursorSel)}, |
| 74 | + {doc: expectDoc}, |
| 75 | + (tr) => expect(get$Cursor(tr.selection)?.pos).toBe(expectCursor), |
| 76 | + ); |
| 77 | + |
| 78 | + const shouldDispatchWithNodeSelection = ( |
| 79 | + initDoc: Node, |
| 80 | + initCursorSel: number, |
| 81 | + expectDoc: Node, |
| 82 | + expectNodeSel: number, |
| 83 | + ) => |
| 84 | + shouldDispatch( |
| 85 | + joinPrevQuote, |
| 86 | + {doc: initDoc, sel: (doc) => TextSelection.create(doc, initCursorSel)}, |
| 87 | + {doc: expectDoc, sel: isNodeSelection}, |
| 88 | + (tr) => expect(tr.selection.from).toBe(expectNodeSel), |
| 89 | + ); |
| 90 | + |
| 91 | + it('should join to textblock in quote', () => { |
| 92 | + shouldDispatchWithCursorSelection( |
| 93 | + doc(bq(p('para in blockqoute')), p('text')), |
| 94 | + 23, // at start of second paragraph |
| 95 | + doc(bq(p('para in blockqoutetext'))), |
| 96 | + 20, |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should join to textblock in nested quotes', () => { |
| 101 | + shouldDispatchWithCursorSelection( |
| 102 | + doc(bq(bq(bq(p('para in nested blockqoutes')))), p('text')), |
| 103 | + 35, // at start of second paragraph |
| 104 | + doc(bq(bq(bq(p('para in nested blockqoutestext'))))), |
| 105 | + 30, |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should join to last textblock in qoute', () => { |
| 110 | + shouldDispatchWithCursorSelection( |
| 111 | + doc(bq(p('first'), p('second')), p('third')), |
| 112 | + 18, // at start of third paragraph |
| 113 | + doc(bq(p('first'), p('secondthird'))), |
| 114 | + 15, |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should select last atom block', () => { |
| 119 | + shouldDispatchWithNodeSelection( |
| 120 | + doc(bq(htmlBlock({[HtmlAttr.Content]: '<div/>'})), p('para')), |
| 121 | + 4, // at start of para |
| 122 | + doc(bq(htmlBlock({[HtmlAttr.Content]: '<div/>'})), p('para')), |
| 123 | + 1, // before html-block |
| 124 | + ); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should select last atom block and remove current empty textblock', () => { |
| 128 | + shouldDispatchWithNodeSelection( |
| 129 | + doc(bq(htmlBlock({[HtmlAttr.Content]: '<div/>'})), p('')), |
| 130 | + 4, // at start of para |
| 131 | + doc(bq(htmlBlock({[HtmlAttr.Content]: '<div/>'}))), |
| 132 | + 1, // before html-block |
| 133 | + ); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should move current textblock to end of last non-qoute block', () => { |
| 137 | + shouldDispatchWithCursorSelection( |
| 138 | + doc(bq(dList(dTerm('Term'), dDesc(p('Definition')))), p('current')), |
| 139 | + 25, // at start of current paragraph |
| 140 | + doc(bq(dList(dTerm('Term'), dDesc(p('Definition'), p('current'))))), |
| 141 | + 22, // at start of current paragraph |
| 142 | + ); |
| 143 | + }); |
| 144 | +}); |
0 commit comments