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