From 5dbe71568e336efb7f491d7cdc3e7939956d5cfe Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Tue, 14 Jun 2022 09:45:22 +0200 Subject: [PATCH] More or less functional --- packages/common/markdown/commands.ts | 4 ++- packages/common/parser.ts | 38 ++++++++++---------- packages/web/boot.ts | 26 +++++++------- packages/web/editor.tsx | 53 ++++++++++++++++++---------- 4 files changed, 70 insertions(+), 51 deletions(-) diff --git a/packages/common/markdown/commands.ts b/packages/common/markdown/commands.ts index 583c857..e1fdc80 100644 --- a/packages/common/markdown/commands.ts +++ b/packages/common/markdown/commands.ts @@ -154,7 +154,9 @@ export const insertNewlineContinueMarkup: StateCommand = ({ { doc } = state; let dont = null, changes = state.changeByRange((range) => { - if (!range.empty || !markdownLanguage.isActiveAt(state, range.from)) + if (!range.empty) + // TODO: Hack due to languagefacet stuff not working + // || !markdownLanguage.isActiveAt(state, range.from)) return (dont = { range }); let pos = range.from, line = doc.lineAt(pos); diff --git a/packages/common/parser.ts b/packages/common/parser.ts index db697aa..ccc22fc 100644 --- a/packages/common/parser.ts +++ b/packages/common/parser.ts @@ -90,25 +90,25 @@ export default function buildMarkdown(mdExtensions: MDExt[]): Language { Comment, Table, ...mdExtensions.map(mdExtensionSyntaxConfig), - parseCode({ - codeParser: getCodeParser([ - LanguageDescription.of({ - name: "yaml", - alias: ["meta", "data"], - support: new LanguageSupport(StreamLanguage.define(yaml)), - }), - LanguageDescription.of({ - name: "javascript", - alias: ["js"], - support: new LanguageSupport(javascriptLanguage), - }), - LanguageDescription.of({ - name: "typescript", - alias: ["ts"], - support: new LanguageSupport(typescriptLanguage), - }), - ]), - }), + // parseCode({ + // codeParser: getCodeParser([ + // LanguageDescription.of({ + // name: "yaml", + // alias: ["meta", "data"], + // support: new LanguageSupport(StreamLanguage.define(yaml)), + // }), + // LanguageDescription.of({ + // name: "javascript", + // alias: ["js"], + // support: new LanguageSupport(javascriptLanguage), + // }), + // LanguageDescription.of({ + // name: "typescript", + // alias: ["ts"], + // support: new LanguageSupport(typescriptLanguage), + // }), + // ]), + // }), { props: [ styleTags({ diff --git a/packages/web/boot.ts b/packages/web/boot.ts index cb131ea..769a0af 100644 --- a/packages/web/boot.ts +++ b/packages/web/boot.ts @@ -55,18 +55,20 @@ safeRun(async () => { }); // if (!isDesktop) { -// if (localStorage.getItem("disable_sw") !== "true") { -if (navigator.serviceWorker) { - navigator.serviceWorker - .register(new URL("service_worker.ts", import.meta.url), { type: "module" }) - .then((r) => { - console.log("Service worker registered..."); - }); -} else { - console.log( - "No launching service worker (not present, maybe because not running on localhost or over SSL)" - ); +if (localStorage.getItem("disable_sw") !== "true") { + if (navigator.serviceWorker) { + navigator.serviceWorker + .register(new URL("service_worker.ts", import.meta.url), { + type: "module", + }) + .then((r) => { + console.log("Service worker registered..."); + }); + } else { + console.log( + "No launching service worker (not present, maybe because not running on localhost or over SSL)" + ); + } } -// } // } diff --git a/packages/web/editor.tsx b/packages/web/editor.tsx index 0519a7b..93c46c6 100644 --- a/packages/web/editor.tsx +++ b/packages/web/editor.tsx @@ -6,13 +6,9 @@ import { import { closeBrackets, closeBracketsKeymap } from "@codemirror/autocomplete"; import { indentWithTab, standardKeymap } from "@codemirror/commands"; import { history, historyKeymap } from "@codemirror/commands"; -import { - bracketMatching, - defaultHighlightStyle, - syntaxHighlighting, -} from "@codemirror/language"; +import { bracketMatching, syntaxHighlighting } from "@codemirror/language"; import { searchKeymap } from "@codemirror/search"; -import { EditorSelection, EditorState } from "@codemirror/state"; +import { Compartment, EditorSelection, EditorState } from "@codemirror/state"; import { drawSelection, dropCursor, @@ -75,6 +71,31 @@ class PageState { const saveInterval = 1000; +// Monkey patching the languageDataAt, somehow the languageData facet is not set +// properly, no idea why +// TODO: Remove at some point +EditorState.prototype.languageDataAt = function ( + name: string, + pos: number, + side = -1 +) { + let values = []; + // console.log("Getting language data"); + // @ts-ignore + for (let provider of this.facet(EditorState.languageData)) { + let providerResult = provider(this, pos, side); + if (!providerResult) { + // console.log("Empty provider result"); + continue; + } + for (let result of providerResult) { + if (Object.prototype.hasOwnProperty.call(result, name)) + values.push(result[name]); + } + } + return values; +}; + export class Editor { readonly commandHook: CommandHook; readonly slashCommandHook: SlashCommandHook; @@ -315,14 +336,15 @@ export class Editor { return EditorState.create({ doc: text, extensions: [ + markdown({ + base: buildMarkdown(this.mdExtensions), + addKeymap: true, + }), highlightSpecialChars(), history(), drawSelection(), dropCursor(), syntaxHighlighting(customMarkdownStyle(this.mdExtensions)), - // syntaxHighlighting(defaultHighlightStyle), - bracketMatching(), - closeBrackets(), autocompletion({ override: [ this.completer.bind(this), @@ -365,14 +387,6 @@ export class Editor { mac: "Cmd-i", run: commands.insertMarker("_"), }, - // { - // key: "Ctrl-p", - // mac: "Cmd-p", - // run: (): boolean => { - // window.open(location.href, "_blank")!.focus(); - // return true; - // }, - // }, { key: "Ctrl-k", mac: "Cmd-k", @@ -439,9 +453,10 @@ export class Editor { } ), pasteLinkExtension, - markdown({ - base: buildMarkdown(this.mdExtensions), + bracketMatching({ + brackets: "()[]{}", }), + closeBrackets(), ], }); }