From da1458e02ad87caeed3b47b5d6e6c398fd7e1bb8 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Thu, 24 Nov 2022 16:55:30 +0100 Subject: [PATCH] Fixes #126 --- web/boot.ts | 2 +- web/editor.tsx | 37 ++++++++++++++++++++----------------- web/styles/editor.scss | 5 ----- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/web/boot.ts b/web/boot.ts index bb59c93c..5b825e91 100644 --- a/web/boot.ts +++ b/web/boot.ts @@ -70,7 +70,7 @@ if (navigator.serviceWorker) { .register(new URL("/service_worker.js", location.href), { type: "module", }) - .then((r) => { + .then(() => { console.log("Service worker registered..."); }); } else { diff --git a/web/editor.tsx b/web/editor.tsx index aa8213d2..6314d310 100644 --- a/web/editor.tsx +++ b/web/editor.tsx @@ -106,6 +106,8 @@ import customMarkdownStyle from "./style.ts"; import { CollabState } from "./cm_plugins/collab.ts"; import { collabSyscalls } from "./syscalls/collab.ts"; +const frontMatterRegex = /^---\s*$(.*?)---\s*$/ms; + class PageState { constructor( readonly scrollTop: number, @@ -295,8 +297,18 @@ export class Editor { scrollIntoView: true, }); } else if (!stateRestored) { + // Somewhat ad-hoc way to determine if the document contains frontmatter and if so, putting the cursor _after it_. + const pageText = this.editorView.state.sliceDoc(); + + // Default the cursor to be at position 0 + let initialCursorPos = 0; + const match = frontMatterRegex.exec(pageText); + if (match) { + // Frotnmatter found, put cursor after it + initialCursorPos = match[0].length; + } this.editorView.dispatch({ - selection: { anchor: 0 }, + selection: { anchor: initialCursorPos }, scrollIntoView: true, }); } @@ -692,10 +704,15 @@ export class Editor { meta: doc.meta, }); + // Note: these events are dispatched asynchronously deliberately (not waiting for results) if (loadingDifferentPage) { - await this.eventHook.dispatchEvent("editor:pageLoaded", pageName); + this.eventHook.dispatchEvent("editor:pageLoaded", pageName).catch( + console.error, + ); } else { - await this.eventHook.dispatchEvent("editor:pageReloaded", pageName); + this.eventHook.dispatchEvent("editor:pageReloaded", pageName).catch( + console.error, + ); } return stateRestored; @@ -709,20 +726,6 @@ export class Editor { "contenteditable", readOnly || this.viewState.forcedROMode ? "false" : "true", ); - - if (isMobileSafari() && readOnly) { - console.log("Safari read only hack"); - contentDOM.classList.add("ios-safari-readonly"); - } else { - contentDOM.classList.remove("ios-safari-readonly"); - } - - function isMobileSafari() { - return ( - navigator.userAgent.match(/(iPod|iPhone|iPad)/) && - navigator.userAgent.match(/AppleWebKit/) - ); - } } private restoreState(pageName: string): boolean { diff --git a/web/styles/editor.scss b/web/styles/editor.scss index 8d51eeae..2f5a58df 100644 --- a/web/styles/editor.scss +++ b/web/styles/editor.scss @@ -16,11 +16,6 @@ outline: none !important; } - // Weird hack to readjust iOS's safari font-size when contenteditable is disabled - .ios-safari-readonly { - font-size: 60%; - } - // Indentation of follow-up lines @mixin lineOverflow($baseIndent, $bulletIndent: 0) { text-indent: -1 * ($baseIndent + 2ch);