|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | +import { expect, test, type Locator, type Page } from '../../fixtures/superdoc.js'; |
| 5 | + |
| 6 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 7 | +const HEADER_DOC_PATH = path.resolve(__dirname, '../../test-data/pagination/longer-header.docx'); |
| 8 | +const FOOTNOTE_DOC_PATH = path.resolve( |
| 9 | + __dirname, |
| 10 | + '../../../../packages/super-editor/src/editors/v1/tests/data/basic-footnotes.docx', |
| 11 | +); |
| 12 | + |
| 13 | +test.skip(!fs.existsSync(HEADER_DOC_PATH), 'Header/footer test document not available — run pnpm corpus:pull'); |
| 14 | +test.skip(!fs.existsSync(FOOTNOTE_DOC_PATH), 'Footnote test document not available'); |
| 15 | + |
| 16 | +test.use({ |
| 17 | + config: { |
| 18 | + comments: 'panel', |
| 19 | + trackChanges: true, |
| 20 | + replacements: 'independent', |
| 21 | + useHiddenHostForStoryParts: true, |
| 22 | + }, |
| 23 | +}); |
| 24 | + |
| 25 | +async function activateHeader(page: Page) { |
| 26 | + const header = page.locator('.superdoc-page-header').first(); |
| 27 | + await header.waitFor({ state: 'visible', timeout: 15_000 }); |
| 28 | + const box = await header.boundingBox(); |
| 29 | + expect(box).toBeTruthy(); |
| 30 | + await page.mouse.dblclick(box!.x + box!.width / 2, box!.y + box!.height / 2); |
| 31 | +} |
| 32 | + |
| 33 | +async function activateFooter(page: Page) { |
| 34 | + const footer = page.locator('.superdoc-page-footer').first(); |
| 35 | + await footer.scrollIntoViewIfNeeded(); |
| 36 | + await footer.waitFor({ state: 'visible', timeout: 15_000 }); |
| 37 | + const box = await footer.boundingBox(); |
| 38 | + expect(box).toBeTruthy(); |
| 39 | + await page.mouse.dblclick(box!.x + box!.width / 2, box!.y + box!.height / 2); |
| 40 | +} |
| 41 | + |
| 42 | +async function activateFootnote(page: Page) { |
| 43 | + const footnote = page.locator('[data-block-id^="footnote-1-"]').first(); |
| 44 | + await footnote.scrollIntoViewIfNeeded(); |
| 45 | + await footnote.waitFor({ state: 'visible', timeout: 15_000 }); |
| 46 | + const box = await footnote.boundingBox(); |
| 47 | + expect(box).toBeTruthy(); |
| 48 | + await page.mouse.dblclick(box!.x + box!.width / 2, box!.y + box!.height / 2); |
| 49 | +} |
| 50 | + |
| 51 | +async function replaceFirstTwoLettersInActiveStory(page: Page, replacementText: string) { |
| 52 | + return page.evaluate( |
| 53 | + ({ replacement }) => { |
| 54 | + const presentation = (window as any).editor?.presentationEditor; |
| 55 | + const hostEditor = (window as any).editor; |
| 56 | + const activeEditor = presentation?.getActiveEditor?.(); |
| 57 | + if (!activeEditor || activeEditor === hostEditor) { |
| 58 | + throw new Error('Expected an active story editor.'); |
| 59 | + } |
| 60 | + |
| 61 | + const storyText = activeEditor.state.doc.textBetween(0, activeEditor.state.doc.content.size, '\n', '\n') ?? ''; |
| 62 | + const match = storyText.match(/[A-Za-z]{2,}/); |
| 63 | + if (!match || match.index == null) { |
| 64 | + throw new Error(`No replaceable word found in active story text: "${storyText}"`); |
| 65 | + } |
| 66 | + |
| 67 | + const deletedText = storyText.slice(match.index, match.index + 2); |
| 68 | + const positions: number[] = []; |
| 69 | + activeEditor.state.doc.descendants((node: any, pos: number) => { |
| 70 | + if (!node?.isText || !node.text) return; |
| 71 | + for (let i = 0; i < node.text.length; i += 1) positions.push(pos + i); |
| 72 | + }); |
| 73 | + |
| 74 | + const from = positions[match.index]; |
| 75 | + const to = positions[match.index + 1] + 1; |
| 76 | + const success = activeEditor.commands.insertTrackedChange({ from, to, text: replacement }); |
| 77 | + |
| 78 | + return { |
| 79 | + success, |
| 80 | + activeDocumentId: activeEditor.options.documentId, |
| 81 | + trackedChanges: activeEditor.options.trackedChanges ?? null, |
| 82 | + deletedText, |
| 83 | + replacement, |
| 84 | + }; |
| 85 | + }, |
| 86 | + { replacement: replacementText }, |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +async function expectIndependentStoryBubbles(page: Page, deletedText: string, insertedText: string) { |
| 91 | + await expect |
| 92 | + .poll( |
| 93 | + () => |
| 94 | + page.evaluate( |
| 95 | + ({ deleted, inserted }) => { |
| 96 | + const comments = (window as any).superdoc?.commentsStore?.commentsList ?? []; |
| 97 | + const trackedChangeComments = comments.filter((comment: any) => comment?.trackedChange); |
| 98 | + const matchingComments = trackedChangeComments.filter( |
| 99 | + (comment: any) => comment?.deletedText === deleted || comment?.trackedChangeText === inserted, |
| 100 | + ); |
| 101 | + const floatingCount = (window as any).superdoc?.commentsStore?.getFloatingComments?.length ?? 0; |
| 102 | + const dialogTexts = Array.from(document.querySelectorAll('.comment-placeholder .comments-dialog')) |
| 103 | + .map((node) => node.textContent ?? '') |
| 104 | + .filter(Boolean); |
| 105 | + |
| 106 | + return { |
| 107 | + matchingTypes: matchingComments.map((comment: any) => comment?.trackedChangeType).sort(), |
| 108 | + matchingDeletedTexts: matchingComments.map((comment: any) => comment?.deletedText).filter(Boolean), |
| 109 | + matchingInsertedTexts: matchingComments.map((comment: any) => comment?.trackedChangeText).filter(Boolean), |
| 110 | + floatingCount, |
| 111 | + dialogTexts, |
| 112 | + }; |
| 113 | + }, |
| 114 | + { deleted: deletedText, inserted: insertedText }, |
| 115 | + ), |
| 116 | + { timeout: 10_000 }, |
| 117 | + ) |
| 118 | + .toEqual( |
| 119 | + expect.objectContaining({ |
| 120 | + matchingTypes: ['trackDelete', 'trackInsert'], |
| 121 | + matchingDeletedTexts: [deletedText], |
| 122 | + matchingInsertedTexts: [insertedText], |
| 123 | + }), |
| 124 | + ); |
| 125 | +} |
| 126 | + |
| 127 | +async function expectActiveStoryReplacementMode(page: Page) { |
| 128 | + await expect |
| 129 | + .poll(() => |
| 130 | + page.evaluate(() => (window as any).editor?.presentationEditor?.getActiveEditor?.()?.options?.trackedChanges), |
| 131 | + ) |
| 132 | + .toEqual( |
| 133 | + expect.objectContaining({ |
| 134 | + replacements: 'independent', |
| 135 | + }), |
| 136 | + ); |
| 137 | +} |
| 138 | + |
| 139 | +test('header replacement sidebar stays independent in suggesting mode', async ({ superdoc }) => { |
| 140 | + await superdoc.loadDocument(HEADER_DOC_PATH); |
| 141 | + await superdoc.waitForStable(); |
| 142 | + await superdoc.setDocumentMode('suggesting'); |
| 143 | + await superdoc.waitForStable(); |
| 144 | + |
| 145 | + await activateHeader(superdoc.page); |
| 146 | + await superdoc.waitForStable(); |
| 147 | + await expectActiveStoryReplacementMode(superdoc.page); |
| 148 | + |
| 149 | + const result = await replaceFirstTwoLettersInActiveStory(superdoc.page, 'x'); |
| 150 | + expect(result.success).toBe(true); |
| 151 | + expect(result.activeDocumentId).not.toBe( |
| 152 | + (await superdoc.page.evaluate(() => (window as any).editor?.options?.documentId)) ?? null, |
| 153 | + ); |
| 154 | + |
| 155 | + await superdoc.waitForStable(); |
| 156 | + await expectIndependentStoryBubbles(superdoc.page, result.deletedText, result.replacement); |
| 157 | +}); |
| 158 | + |
| 159 | +test('footer replacement sidebar stays independent in suggesting mode', async ({ superdoc }) => { |
| 160 | + await superdoc.loadDocument(HEADER_DOC_PATH); |
| 161 | + await superdoc.waitForStable(); |
| 162 | + await superdoc.setDocumentMode('suggesting'); |
| 163 | + await superdoc.waitForStable(); |
| 164 | + |
| 165 | + await activateFooter(superdoc.page); |
| 166 | + await superdoc.waitForStable(); |
| 167 | + await expectActiveStoryReplacementMode(superdoc.page); |
| 168 | + |
| 169 | + const result = await replaceFirstTwoLettersInActiveStory(superdoc.page, 'x'); |
| 170 | + expect(result.success).toBe(true); |
| 171 | + |
| 172 | + await superdoc.waitForStable(); |
| 173 | + await expectIndependentStoryBubbles(superdoc.page, result.deletedText, result.replacement); |
| 174 | +}); |
| 175 | + |
| 176 | +test('footnote replacement sidebar stays independent in suggesting mode', async ({ superdoc }) => { |
| 177 | + await superdoc.loadDocument(FOOTNOTE_DOC_PATH); |
| 178 | + await superdoc.waitForStable(); |
| 179 | + await superdoc.setDocumentMode('suggesting'); |
| 180 | + await superdoc.waitForStable(); |
| 181 | + |
| 182 | + await activateFootnote(superdoc.page); |
| 183 | + await superdoc.waitForStable(); |
| 184 | + await expectActiveStoryReplacementMode(superdoc.page); |
| 185 | + |
| 186 | + const result = await replaceFirstTwoLettersInActiveStory(superdoc.page, 'x'); |
| 187 | + expect(result.success).toBe(true); |
| 188 | + |
| 189 | + await superdoc.waitForStable(); |
| 190 | + await expectIndependentStoryBubbles(superdoc.page, result.deletedText, result.replacement); |
| 191 | +}); |
0 commit comments