From 44dfcb9fe3014d724b3c64249e3be051f8664cbc Mon Sep 17 00:00:00 2001 From: bakuzan Date: Mon, 30 Jan 2023 11:12:39 +0000 Subject: [PATCH 1/2] Work around for undefined user event type. Fixes #69. --- src/editor/EditorPlugin.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/editor/EditorPlugin.ts b/src/editor/EditorPlugin.ts index 28c429f..19d81a8 100644 --- a/src/editor/EditorPlugin.ts +++ b/src/editor/EditorPlugin.ts @@ -1,3 +1,4 @@ +import { Transaction } from "@codemirror/state"; import { ViewUpdate, PluginValue, @@ -22,9 +23,19 @@ class EditorPlugin implements PluginValue { } const tr = update.transactions[0]; - if (!tr) return; + + if (!tr) { + return; + } + + // When selecting text with Shift+Home the userEventType is undefined. + // This is probably a bug in codemirror, for the time being doing an explict check + // for the type allows us to update the stats for the selection. + const userEventTypeUndefined = + tr.annotation(Transaction.userEvent) === undefined; + if ( - tr.isUserEvent("select") && + (tr.isUserEvent("select") || userEventTypeUndefined) && tr.newSelection.ranges[0].from !== tr.newSelection.ranges[0].to ) { let text = ""; @@ -42,6 +53,7 @@ class EditorPlugin implements PluginValue { tr.isUserEvent("redo") || tr.isUserEvent("select") ) { + console.log("OTHER EVENTS"); const textIter = tr.newDoc.iter(); let text = ""; while (!textIter.done) { From 9cdab1d1d71548b1256a87a67768c015d879eee9 Mon Sep 17 00:00:00 2001 From: bakuzan Date: Mon, 30 Jan 2023 11:14:14 +0000 Subject: [PATCH 2/2] Remove log --- src/editor/EditorPlugin.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/editor/EditorPlugin.ts b/src/editor/EditorPlugin.ts index 19d81a8..2626e34 100644 --- a/src/editor/EditorPlugin.ts +++ b/src/editor/EditorPlugin.ts @@ -53,7 +53,6 @@ class EditorPlugin implements PluginValue { tr.isUserEvent("redo") || tr.isUserEvent("select") ) { - console.log("OTHER EVENTS"); const textIter = tr.newDoc.iter(); let text = ""; while (!textIter.done) {