Skip to content

Commit

Permalink
More performance - Bump prose init up in call stack
Browse files Browse the repository at this point in the history
  • Loading branch information
auniverseaway committed Jan 13, 2025
1 parent 328800e commit 7c8dfe6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
4 changes: 4 additions & 0 deletions blocks/edit/da-content/da-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export default class DaContent extends LitElement {
static properties = {
details: { attribute: false },
permissions: { attribute: false },
proseEl: { attribute: false },
wsProvider: { attribute: false },
_editorLoaded: { state: true },
_versionUrl: { state: true },
};
Expand Down Expand Up @@ -66,6 +68,8 @@ export default class DaContent extends LitElement {
path="${this.details.sourceUrl}"
version="${this._versionUrl}"
.permissions=${this.permissions}
.proseEl=${this.proseEl}
.wsProvider=${this.wsProvider}
@proseloaded=${this.handleEditorLoaded}
@versionreset=${this.handleReset}>
</da-editor>
Expand Down
31 changes: 9 additions & 22 deletions blocks/edit/da-editor/da-editor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DOMParser as proseDOMParser } from 'da-y-wrapper';
import { LitElement, html, nothing } from 'da-lit';
import initProse from '../prose/index.js';
import getSheet from '../../shared/sheet.js';
import { initIms, daFetch } from '../../shared/utils.js';
import { parse, aem2prose } from '../utils/helpers.js';
Expand All @@ -11,6 +10,8 @@ export default class DaEditor extends LitElement {
static properties = {
path: { type: String },
version: { type: String },
proseEl: { attribute: false },
wsProvider: { attribute: false },
permissions: { state: true },
_imsLoaded: { state: false },
_versionDom: { state: true },
Expand All @@ -23,18 +24,6 @@ export default class DaEditor extends LitElement {
initIms().then(() => { this._imsLoaded = true; });
}

disconnectWebsocket() {
if (this.wsProvider) {
this.wsProvider.disconnect({ data: 'Client navigation' });
this.wsProvider = undefined;
}
}

disconnectedCallback() {
super.disconnectedCallback();
this.disconnectWebsocket();
}

async fetchVersion() {
this._versionDom = null;
const resp = await daFetch(this.version);
Expand Down Expand Up @@ -70,6 +59,10 @@ export default class DaEditor extends LitElement {
this.handleCancel();
}

get _proseEl() {
return this.shadowRoot.querySelector('.da-prose-mirror');
}

renderVersion() {
return html`
<div class="da-prose-mirror da-version-preview">
Expand All @@ -83,7 +76,6 @@ export default class DaEditor extends LitElement {

render() {
return html`
<div class="da-prose-mirror"></div>
${this._versionDom ? this.renderVersion() : nothing}
`;
}
Expand All @@ -94,14 +86,9 @@ export default class DaEditor extends LitElement {
}

// Do not setup prosemirror until we know the permissions
if (props.has('permissions')) {
if (this.path && this.permissions) {
this.disconnectWebsocket();
const editor = this.shadowRoot.querySelector('.da-prose-mirror');
editor.innerHTML = '';

this.wsProvider = initProse({ editor, path: this.path, permissions: this.permissions });
}
if (props.has('proseEl') && this.path && this.permissions) {
if (this._proseEl) this._proseEl.remove();
this.shadowRoot.append(this.proseEl);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions blocks/edit/edit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import getPathDetails from '../shared/pathDetails.js';
import initProse from './prose/index.js';

let proseEl;
let wsProvider;

async function setUI(el, utils) {
const details = getPathDetails();
Expand Down Expand Up @@ -34,6 +38,12 @@ async function setUI(el, utils) {
const { permissions } = await daFetch(details.sourceUrl, { method: 'HEAD' });
daTitle.permissions = permissions;
daContent.permissions = permissions;

if (wsProvider) wsProvider.disconnect({ data: 'Client navigation' });

({ proseEl, wsProvider } = initProse({ path: details.sourceUrl, permissions }));
daContent.proseEl = proseEl;
daContent.wsProvider = wsProvider;
}

export default async function init(el) {
Expand Down
11 changes: 5 additions & 6 deletions blocks/edit/prose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,11 @@ function restoreCursorPosition(view) {
}
}

export default function initProse({ editor, path, permissions }) {
export default function initProse({ path, permissions }) {
// Destroy ProseMirror if it already exists - GH-212
if (window.view) delete window.view;
const editor = document.createElement('div');
editor.className = 'da-prose-mirror';

const schema = getSchema();

Expand Down Expand Up @@ -260,10 +262,7 @@ export default function initProse({ editor, path, permissions }) {
plugins.push(menu);
}

let state = EditorState.create({
schema,
plugins,
});
let state = EditorState.create({ schema, plugins });

const fix = fixTables(state);
if (fix) state = state.apply(fix.setMeta('addToHistory', false));
Expand Down Expand Up @@ -299,5 +298,5 @@ export default function initProse({ editor, path, permissions }) {
document.execCommand('enableObjectResizing', false, 'false');
document.execCommand('enableInlineTableEditing', false, 'false');

return wsProvider;
return { proseEl: editor, wsProvider };
}

0 comments on commit 7c8dfe6

Please sign in to comment.