Skip to content
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

Regenerate da-y-wrapper and fix the preview window updates #214

Merged
merged 7 commits into from
Sep 24, 2024
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
16 changes: 11 additions & 5 deletions blocks/edit/prose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export function getSchema() {
return new Schema({ nodes, marks: addCustomMarks(marks) });
}

let pollerSetUp = false;
let sendUpdates = false;
let hasChanged = 0;
function dispatchTransaction(transaction) {
Expand All @@ -116,11 +117,12 @@ function setPreviewBody(daPreview, proseEl) {
daPreview.body = body;
}

function pollForUpdates() {
const daContent = document.querySelector('da-content');
const daPreview = daContent.shadowRoot.querySelector('da-preview');
if (!window.view) return;
const proseEl = window.view.root.querySelector('.ProseMirror');
export function pollForUpdates(doc = document, win = window) {
Copy link
Contributor

@chrischrischris chrischrischris Sep 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion - I prefer using an IIFE to contain the pollerSetup state outside global scope:

const pollForUpdates = (() => {
  let pollerSetUp = false;

  return (doc = document, win = window) => {
    if (pollerSetUp) return;
    const daContent = doc.querySelector('da-content');
    const daPreview = daContent?.shadowRoot.querySelector('da-preview');
    if (!win.view) return;
    const proseEl = win.view.root.querySelector('.ProseMirror');
    if (!daPreview) return;

    setInterval(() => {
      if (sendUpdates) {
        if (hasChanged > 0) {
          hasChanged = 0;
          return;
        }
        setPreviewBody(daPreview, proseEl);
        sendUpdates = false;
      }
    }, 500);
    pollerSetUp = true;
  };
})();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I will see if I can do something like this in a separate commit.

if (pollerSetUp) return;
const daContent = doc.querySelector('da-content');
const daPreview = daContent?.shadowRoot.querySelector('da-preview');
if (!win.view) return;
const proseEl = win.view.root.querySelector('.ProseMirror');
if (!daPreview) return;

setInterval(() => {
Expand All @@ -133,6 +135,7 @@ function pollForUpdates() {
sendUpdates = false;
}
}, 500);
pollerSetUp = true;
}

function handleAwarenessUpdates(wsProvider, daTitle, win) {
Expand Down Expand Up @@ -299,6 +302,9 @@ export default function initProse({ editor, path }) {
},
});

// Call pollForUpdates() to make sure it gets called even if the callback was made earlier
pollForUpdates();

document.execCommand('enableObjectResizing', false, 'false');
document.execCommand('enableInlineTableEditing', false, 'false');

Expand Down
Loading
Loading