8000 Use document GUID as prosemirror reference to avoid updating deleted documents by bosschaert · Pull Request #450 · adobe/da-live · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use document GUID as prosemirror reference to avoid updating deleted documents #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions blocks/edit/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import initProse from './prose/index.js';
let proseEl;
let wsProvider;

async function setUI(el, utils) {
async function setUI(el, utils, guid) {
const details = getPathDetails();
if (!details) {
// el.classList.add('no-url');
Expand Down Expand Up @@ -35,7 +35,7 @@ async function setUI(el, utils) {
}

const { daFetch } = await utils;
const { permissions } = await daFetch(details.sourceUrl, { method: 'HEAD' });
const { permissions, headers } = await daFetch(details.sourceUrl, { method: 'HEAD' });
daTitle.permissions = permissions;
daContent.permissions = permissions;

Expand All @@ -44,7 +44,12 @@ async function setUI(el, utils) {
daContent.wsProvider = undefined;
}

({ proseEl, wsProvider } = initProse({ path: details.sourceUrl, permissions }));
const docguid = guid ?? headers?.get('x-da-id');
({ proseEl, wsProvider } = initProse({
path: details.sourceUrl,
permissions,
docguid,
}, (updatedGuid) => setUI(el, utils, updatedGuid)));
daContent.proseEl = proseEl;
daContent.wsProvider = wsProvider;
}
Expand Down
27 changes: 25 additions & 2 deletions blocks/edit/prose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function addSyncedListener(wsProvider) {
});
}

export default function initProse({ path, permissions }) {
export default function initProse({ path, permissions, docguid }, resetFunc) {
// Destroy ProseMirror if it already exists - GH-212
if (window.view) delete window.view;
const editor = document.createElement('div');
Expand All @@ -212,7 +212,30 @@ export default function initProse({ path, permissions }) {
createAwarenessStatusWidget(wsProvider, window);
registerErrorHandler(ydoc);

const yXmlFragment = ydoc.getXmlFragment('prosemirror');
const guidArray = ydoc.getArray('prosemirror-guids');
let curGuid;
if (docguid) {
curGuid = docguid;
} else {
curGuid = crypto.randomUUID();
guidArray.push([{ ts: Date.now(), guid: curGuid, newDoc: true }]);
}

ydoc.on('update', () => {
// If the document has been replaced by a another document (it has been first deleted
// and then a new document has been created), reset the window to connect to the new doc.
const guids = [...guidArray];
if (guids.length === 0) {
return;
}
guids.sort((a, b) => a.ts - b.ts);
const latestGuid = guids.pop();
if (latestGuid.guid !== curGuid) {
resetFunc(latestGuid.guid);
}
});

const yXmlFragment = ydoc.getXmlFragment(`prosemirror-${curGuid}`);

if (window.adobeIMS?.isSignedInUser()) {
window.adobeIMS.getProfile().then(
Expand Down
13 changes: 12 additions & 1 deletion test/e2e/tests/delete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ test('Empty out open editors on deleted documents', async ({ browser, page }, wo
const enteredText = `Some content entered at ${new Date()}`;
await page.locator('div.ProseMirror').fill(enteredText);

// Wait for the content to be stored in the backend
await page.waitForTimeout(3000);

// Create a second window on the same document
const page2 = await browser.newPage();
await page2.goto(url);
Expand All @@ -119,8 +122,16 @@ test('Empty out open editors on deleted documents', async ({ browser, page }, wo
await list.locator('button.delete-button').locator('visible=true').click();

// Give the second window a chance to update itself
await list.waitForTimeout(10000);
await page2.waitForTimeout(3000);

// The open window should be cleared out now
await expect(page2.locator('div.ProseMirror')).not.toContainText(enteredText);

// Add some text to the second window with the stale document, it should not be saved
await page2.locator('div.ProseMirror').fill('Some new content');
await page2.waitForTimeout(5000);

list.reload();
// The document should still not be in the list, even though edits were made on the stale doc
await expect(list.locator(`a[href="/edit#/da-sites/da-status/tests/${pageName}"]`)).not.toBeVisible();
});
4 changes: 2 additions & 2 deletions test/unit/scripts/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { setNx } from '../../../scripts/utils.js';
describe('Libs', () => {
it('Default Libs', () => {
const libs = setNx('/nx');
expect(libs).to.equal('https://main--nexter--da-sites.aem.live/nx');
expect(libs).to.equal('https://main--da-nx--adobe.aem.live/nx');
});

it('Does not support NX query param on prod', () => {
Expand All @@ -22,7 +22,7 @@ describe('Libs', () => {
search: '?nx=foo',
};
const libs = setNx('/nx', location);
expect(libs).to.equal('https://foo--nexter--da-sites.aem.live/nx');
expect(libs).to.equal('https://foo--da-nx--adobe.aem.live/nx');
});

it('Supports local NX query param', () => {
Expand Down
Loading
0