|
| 1 | +/* eslint-disable no-undef */ |
| 2 | + |
| 3 | +import { section, heading, paragraph } from '../../../fixtures/docMaker' |
| 4 | + |
| 5 | +const moveOpts = { keepMouseDownButton: 'left', scrollBehavior: false } |
| 6 | + |
| 7 | +const row = (text) => cy.getTocItem(text).find('> .toc__row') |
| 8 | +const grip = (text) => row(text).find('.toc-drag-handle') |
| 9 | + |
| 10 | +/** Presses the grip and moves past dnd-kit's 5px start distance. */ |
| 11 | +const pickUp = (text) => { |
| 12 | + row(text).realHover({ scrollBehavior: false }) |
| 13 | + grip(text).then(($grip) => { |
| 14 | + const box = $grip[0].getBoundingClientRect() |
| 15 | + grip(text).realMouseDown({ position: 'center', scrollBehavior: false }) |
| 16 | + cy.get('body').realMouseMove(box.left + box.width / 2, box.top + box.height / 2 + 12, moveOpts) |
| 17 | + }) |
| 18 | + cy.get('.toc__list.is-dragging').should('exist') |
| 19 | +} |
| 20 | + |
| 21 | +/** Rects are read after pickup, because the ghosted rows grow. */ |
| 22 | +const hoverAbove = (text) => |
| 23 | + row(text).then(($row) => { |
| 24 | + const box = $row[0].getBoundingClientRect() |
| 25 | + cy.get('body').realMouseMove(box.left + 40, box.top + 3, moveOpts) |
| 26 | + }) |
| 27 | + |
| 28 | +const hoverBelow = (text) => |
| 29 | + row(text).then(($row) => { |
| 30 | + const box = $row[0].getBoundingClientRect() |
| 31 | + cy.get('body').realMouseMove(box.left + 40, box.bottom - 3, moveOpts) |
| 32 | + }) |
| 33 | + |
| 34 | +const drop = () => |
| 35 | + cy.get('.toc-drop-indicator-portal').then(($line) => { |
| 36 | + const y = Number($line.attr('data-y')) |
| 37 | + cy.get('body').realMouseUp({ x: 60, y, scrollBehavior: false }) |
| 38 | + }) |
| 39 | + |
| 40 | +const outline = () => |
| 41 | + cy.window().then((win) => { |
| 42 | + const out = [] |
| 43 | + win._editor.state.doc.forEach((node) => { |
| 44 | + if (node.type.name === 'heading') out.push(`${node.attrs.level}:${node.textContent}`) |
| 45 | + }) |
| 46 | + return out |
| 47 | + }) |
| 48 | + |
| 49 | +describe('TOC drag by pointer', () => { |
| 50 | + beforeEach(() => { |
| 51 | + cy.visitEditor() |
| 52 | + cy.createDocument({ |
| 53 | + sections: [ |
| 54 | + section('Pointer Title', [ |
| 55 | + heading(2, 'Alpha', [ |
| 56 | + paragraph('alpha body'), |
| 57 | + heading(3, 'Alpha One', [ |
| 58 | + paragraph('alpha one body'), |
| 59 | + heading(4, 'Alpha One Deep', [paragraph('deep body')]) |
| 60 | + ]) |
| 61 | + ]), |
| 62 | + heading(2, 'Bravo', [ |
| 63 | + paragraph('bravo body'), |
| 64 | + heading(3, 'Bravo One', [paragraph('bravo one body')]) |
| 65 | + ]), |
| 66 | + heading(2, 'Charlie', [paragraph('charlie body')]) |
| 67 | + ]) |
| 68 | + ] |
| 69 | + }) |
| 70 | + cy.waitForToc() |
| 71 | + }) |
| 72 | + |
| 73 | + it('gives the Title row no grip', () => { |
| 74 | + row('Pointer Title').should('exist').find('.toc-drag-handle').should('not.exist') |
| 75 | + grip('Bravo').should('exist') |
| 76 | + }) |
| 77 | + |
| 78 | + it('derives the level from the gap and moves sub-headings with a demoted section', () => { |
| 79 | + pickUp('Bravo') |
| 80 | + hoverAbove('Alpha One') |
| 81 | + // DS-12: the landing parent's rail turns primary; every other rail stays stock. |
| 82 | + cy.getTocItem('Alpha') |
| 83 | + .find('> ul.toc__children') |
| 84 | + .should('have.class', 'is-drop-parent') |
| 85 | + .then(($ul) => { |
| 86 | + const win = $ul[0].ownerDocument.defaultView |
| 87 | + const rail = win.getComputedStyle($ul[0], '::before') |
| 88 | + const stock = win.getComputedStyle( |
| 89 | + $ul[0].ownerDocument.querySelector('ul.toc__children:not(.is-drop-parent)'), |
| 90 | + '::before' |
| 91 | + ) |
| 92 | + expect(rail.opacity).to.equal('1') |
| 93 | + expect(stock.opacity).not.to.equal('1') |
| 94 | + expect(rail.backgroundColor).not.to.equal(stock.backgroundColor) |
| 95 | + }) |
| 96 | + cy.get('ul.toc__children.is-drop-parent').should('have.length', 1) |
| 97 | + drop() |
| 98 | + |
| 99 | + cy.get('ul.toc__children.is-drop-parent').should('not.exist') |
| 100 | + outline().should('deep.equal', [ |
| 101 | + '1:Pointer Title', |
| 102 | + '2:Alpha', |
| 103 | + '3:Bravo', |
| 104 | + '4:Bravo One', |
| 105 | + '3:Alpha One', |
| 106 | + '4:Alpha One Deep', |
| 107 | + '2:Charlie' |
| 108 | + ]) |
| 109 | + }) |
| 110 | + |
| 111 | + it('moves sub-headings with a promoted section', () => { |
| 112 | + pickUp('Alpha One') |
| 113 | + hoverAbove('Bravo') |
| 114 | + // A root-level child of the Title lands under the Title's own rail. |
| 115 | + cy.getTocItem('Pointer Title').find('> ul.toc__children').should('have.class', 'is-drop-parent') |
| 116 | + drop() |
| 117 | + |
| 118 | + outline().should('deep.equal', [ |
| 119 | + '1:Pointer Title', |
| 120 | + '2:Alpha', |
| 121 | + '2:Alpha One', |
| 122 | + '3:Alpha One Deep', |
| 123 | + '2:Bravo', |
| 124 | + '3:Bravo One', |
| 125 | + '2:Charlie' |
| 126 | + ]) |
| 127 | + }) |
| 128 | + |
| 129 | + it('keeps the caret in the moved text', () => { |
| 130 | + cy.window().then((win) => { |
| 131 | + const editor = win._editor |
| 132 | + let caret = null |
| 133 | + editor.state.doc.descendants((node, pos) => { |
| 134 | + if (caret === null && node.isText && node.text === 'bravo body') caret = pos + 3 |
| 135 | + }) |
| 136 | + editor.commands.setTextSelection(caret) |
| 137 | + }) |
| 138 | + |
| 139 | + pickUp('Bravo') |
| 140 | + hoverAbove('Alpha') |
| 141 | + drop() |
| 142 | + |
| 143 | + outline().should('deep.equal', [ |
| 144 | + '1:Pointer Title', |
| 145 | + '2:Bravo', |
| 146 | + '3:Bravo One', |
| 147 | + '2:Alpha', |
| 148 | + '3:Alpha One', |
| 149 | + '4:Alpha One Deep', |
| 150 | + '2:Charlie' |
| 151 | + ]) |
| 152 | + cy.window().then((win) => { |
| 153 | + const { $from } = win._editor.state.selection |
| 154 | + expect($from.parent.textContent).to.equal('bravo body') |
| 155 | + expect($from.parentOffset).to.equal(3) |
| 156 | + }) |
| 157 | + }) |
| 158 | + |
| 159 | + it('lands a drop below a folded row after its whole section', () => { |
| 160 | + row('Alpha').find('button.toc__fold-btn').click({ force: true }) |
| 161 | + row('Alpha One').should('not.be.visible') |
| 162 | + |
| 163 | + pickUp('Charlie') |
| 164 | + hoverBelow('Alpha') |
| 165 | + drop() |
| 166 | + |
| 167 | + outline().should('deep.equal', [ |
| 168 | + '1:Pointer Title', |
| 169 | + '2:Alpha', |
| 170 | + '3:Alpha One', |
| 171 | + '4:Alpha One Deep', |
| 172 | + '2:Charlie', |
| 173 | + '2:Bravo', |
| 174 | + '3:Bravo One' |
| 175 | + ]) |
| 176 | + }) |
| 177 | + |
| 178 | + it('shows no grip on a locked pad', () => { |
| 179 | + grip('Bravo').should('exist') |
| 180 | + cy.window().then((win) => { |
| 181 | + win._store.getState().setWorkspaceSetting('authorizedScope', 'readonly') |
| 182 | + }) |
| 183 | + cy.get('.toc__list .toc-drag-handle').should('not.exist') |
| 184 | + }) |
| 185 | + |
| 186 | + it('refuses a drop above the Title', () => { |
| 187 | + pickUp('Charlie') |
| 188 | + hoverAbove('Pointer Title') |
| 189 | + cy.get('.toc-drop-indicator-portal').should('not.exist') |
| 190 | + cy.get('body').should('have.css', 'cursor', 'no-drop') |
| 191 | + cy.get('body').realMouseUp({ x: 60, y: 5, scrollBehavior: false }) |
| 192 | + cy.get('body').should('not.have.css', 'cursor', 'no-drop') |
| 193 | + outline().its(1).should('equal', '2:Alpha') |
| 194 | + }) |
| 195 | +}) |
0 commit comments