Bento, the office suite that fits in a file
TypeScript mit-licenseofficeoffice-365office-suiteoffice-toolsoffice365open-sourcepowerpointself-hostedsingle-page-appslidesslideshareslideshowslideshow-galleryslideshow-makerslideshows
slides: Save perf improvements (phase 1) (#792)
Rahul Ravikumarb6fa739b9dd6
diff --git a/kernel/src/save.ts b/kernel/src/save.tsindex a14044ea..78a2b09c 100644--- a/kernel/src/save.ts+++ b/kernel/src/save.ts@@ -178,7 +178,7 @@ function serializeBody(shell: Document, body: string, doc: KernelDoc): string {el.id = b.idfor (const [k, v] of Object.entries(b.attrs ?? {})) el.setAttribute(k, v)// same <-escape as the doc block: these can never contain "</script>"- el.textContent = '\n' + b.body.replace(/</g, '\\u003c') + '\n'+ el.textContent = '\n' + (b.body.includes('<') ? b.body.replace(/</g, '\\u003c') : b.body) + '\n'clone.head.appendChild(el)}@@ -190,7 +190,7 @@ function serializeBody(shell: Document, body: string, doc: KernelDoc): string {clone.head.appendChild(block)}// <-escape so the JSON can never contain "</script>" and break the file.- block.textContent = '\n' + body.replace(/</g, '\\u003c') + '\n'+ block.textContent = '\n' + (body.includes('<') ? body.replace(/</g, '\\u003c') : body) + '\n'writePreview(clone, body, doc)@@ -211,7 +211,15 @@ function serializeBody(shell: Document, body: string, doc: KernelDoc): string {const html = '<!DOCTYPE html>\n' + clone.documentElement.outerHTML// Belt-and-braces: an unescaped close tag anywhere in generated output would// corrupt the file; this should never trigger given the escaping above.- if (html.split(SCRIPT_CLOSE).length !== clone.querySelectorAll('script').length + 1) {+ // Count occurrences with indexOf instead of splitting the multi-megabyte string into an array.+ let scripts = 0+ let position = 0+ // Use `indexOf` here to avoid allocations galore.+ while ((position = html.indexOf(SCRIPT_CLOSE, position)) !== -1) {+ scripts += 1+ position += SCRIPT_CLOSE.length+ }+ if (scripts !== clone.querySelectorAll('script').length) {console.warn('bento: unexpected script-close count in serialized file')}return htmldiff --git a/kernel/src/sync/session.ts b/kernel/src/sync/session.tsindex f2f696ab..4fdeb037 100644--- a/kernel/src/sync/session.ts+++ b/kernel/src/sync/session.ts@@ -35,6 +35,7 @@ export type { BlobRef, CollabCreds, SyncDoc }import { putBlob, getBlob, dataUriToBytes, bytesToDataUri, encodedSize, MAX_BLOB } from './blobs.ts'import { mintCollab, cachedMemberPub } from './online.ts'import { lsGet, lsJson } from '../storage.ts'+import { copy } from '../documentvalue.ts'/*** The store the session drives, reduced to what it actually touches.@@ -1132,7 +1133,7 @@ export class SyncSession {stampInto(doc: SyncDoc) {if (!doc.collab || doc.collab.on === false) returnthis.flush()- doc.collab.sync = JSON.parse(JSON.stringify(this.state.toJSON()))+ doc.collab.sync = copy(this.state.toJSON())}// --- plumbing -------------------------------------------------------------diff --git a/slides/src/editor/editor.ts b/slides/src/editor/editor.tsindex aa3020e1..4ec959f9 100644--- a/slides/src/editor/editor.ts+++ b/slides/src/editor/editor.ts@@ -3187,8 +3187,10 @@ export class Editor {// Saving is the opt-in: a named, saved deck is "live by default" from// now on (the recipient of a copy already joins on open). Connect this// session too so author and recipient meet without another click.- this.session?.enableSharing()- this.tryJoin()+ if (sharingOn(this.store)) {+ this.session?.enableSharing()+ this.tryJoin()+ }// A save from the WEB changes what this page is: the deck now lives in a// file, and this URL will hand out a fresh starter next time. Said here,// persistently, so the next visit is not a surprise — and deliberately