From a96c52b21e7e590bbdd70b80896780a446fa2e8b Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Mon, 13 Dec 2021 02:07:04 +0100 Subject: build: separate module for frontend This allows us to simplify the webpack configuration and the gradle build scripts. --- .../frontend/src/editor/decorationSetExtension.ts | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 subprojects/frontend/src/editor/decorationSetExtension.ts (limited to 'subprojects/frontend/src/editor/decorationSetExtension.ts') diff --git a/subprojects/frontend/src/editor/decorationSetExtension.ts b/subprojects/frontend/src/editor/decorationSetExtension.ts new file mode 100644 index 00000000..2d630c20 --- /dev/null +++ b/subprojects/frontend/src/editor/decorationSetExtension.ts @@ -0,0 +1,39 @@ +import { StateEffect, StateField, TransactionSpec } from '@codemirror/state'; +import { EditorView, Decoration, DecorationSet } from '@codemirror/view'; + +export type TransactionSpecFactory = (decorations: DecorationSet) => TransactionSpec; + +export function decorationSetExtension(): [TransactionSpecFactory, StateField] { + const setEffect = StateEffect.define(); + const field = StateField.define({ + create() { + return Decoration.none; + }, + update(currentDecorations, transaction) { + let newDecorations: DecorationSet | null = null; + transaction.effects.forEach((effect) => { + if (effect.is(setEffect)) { + newDecorations = effect.value; + } + }); + if (newDecorations === null) { + if (transaction.docChanged) { + return currentDecorations.map(transaction.changes); + } + return currentDecorations; + } + return newDecorations; + }, + provide: (f) => EditorView.decorations.from(f), + }); + + function transactionSpecFactory(decorations: DecorationSet) { + return { + effects: [ + setEffect.of(decorations), + ], + }; + } + + return [transactionSpecFactory, field]; +} -- cgit v1.2.3-70-g09d2