From f3f5a422189f8d032f8a2e8fd3f934d318d6fbed Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Sat, 9 Apr 2022 05:25:54 +0200 Subject: [PATCH] Speed up editor by lazily loading CodeMirror - Second try (#1776) * Revert "Revert "Speed up editor by lazily loading CodeMirror (#1766)"" This reverts commit b0a2884f192d0c3f5b1a70ffd4fd70305a24f070. * Pass in options via action update + Make access to editor optional --- ts/editor/CodeMirror.svelte | 12 +++++--- ts/editor/code-mirror.ts | 28 ++++++++++++++----- .../plain-text-input/PlainTextInput.svelte | 8 +++++- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/ts/editor/CodeMirror.svelte b/ts/editor/CodeMirror.svelte index 204d22161..f9eaeeb41 100644 --- a/ts/editor/CodeMirror.svelte +++ b/ts/editor/CodeMirror.svelte @@ -30,6 +30,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html export let configuration: CodeMirrorLib.EditorConfiguration; export let code: Writable; + export let hidden = false; const defaultConfiguration = { rtlMoveVisually: true, @@ -50,9 +51,6 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html const direction = getContext>(directionKey); - $: setOption("direction", $direction); - $: setOption("theme", $pageTheme.isDark ? darkTheme : lightTheme); - let apiPartial: Partial; export { apiPartial as api }; @@ -77,8 +75,14 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html tabindex="-1" hidden use:openCodeMirror={{ - configuration: { ...configuration, ...defaultConfiguration }, + configuration: { + ...configuration, + ...defaultConfiguration, + direction: $direction, + theme: $pageTheme.isDark ? darkTheme : lightTheme, + }, resolve, + hidden, }} /> diff --git a/ts/editor/code-mirror.ts b/ts/editor/code-mirror.ts index f9dbb9d3e..a11becc83 100644 --- a/ts/editor/code-mirror.ts +++ b/ts/editor/code-mirror.ts @@ -63,26 +63,40 @@ export function focusAndSetCaret( interface OpenCodeMirrorOptions { configuration: CodeMirror.EditorConfiguration; resolve(editor: CodeMirror.EditorFromTextArea): void; + hidden: boolean; } export function openCodeMirror( textarea: HTMLTextAreaElement, - { configuration, resolve }: Partial, + options: Partial, ): { update: (options: Partial) => void; destroy: () => void } { - const editor = CodeMirror.fromTextArea(textarea, configuration); - resolve?.(editor); + let editor: CodeMirror.EditorFromTextArea | null = null; - return { - update({ configuration }: Partial): void { + function update({ + configuration, + resolve, + hidden, + }: Partial): void { + if (editor) { for (const key in configuration) { editor.setOption( key as keyof CodeMirror.EditorConfiguration, configuration[key], ); } - }, + } else if (!hidden) { + editor = CodeMirror.fromTextArea(textarea, configuration); + resolve?.(editor); + } + } + + update(options); + + return { + update, destroy(): void { - editor.toTextArea(); + editor?.toTextArea(); + editor = null; }, }; } diff --git a/ts/editor/plain-text-input/PlainTextInput.svelte b/ts/editor/plain-text-input/PlainTextInput.svelte index 7556d9364..c639ffa76 100644 --- a/ts/editor/plain-text-input/PlainTextInput.svelte +++ b/ts/editor/plain-text-input/PlainTextInput.svelte @@ -136,7 +136,13 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html class:hidden on:focusin={() => ($focusedInput = api)} > - +