From ebd84cf488dc26ffc705be7ba0f8b403633fc4a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Fri, 27 Nov 2020 14:52:19 +0100 Subject: [PATCH] add onSelection event --- docs/src/docPages/api/events.md | 11 +++++++++-- packages/core/src/Editor.ts | 19 +++++++------------ packages/core/src/commands/focus.ts | 2 +- packages/core/src/types.ts | 6 +----- 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/docs/src/docPages/api/events.md b/docs/src/docPages/api/events.md index 0f696257..0be726bc 100644 --- a/docs/src/docPages/api/events.md +++ b/docs/src/docPages/api/events.md @@ -16,6 +16,9 @@ const editor = new Editor({ onUpdate() { // The content has changed. }, + onSelection() { + // The selection has changed. + }, onTransaction({ transaction }) { // The editor state has changed. }, @@ -41,15 +44,19 @@ editor.on('update', () => { // The content has changed. } +editor.on('selection', () => { + // The selection has changed. +} + editor.on('transaction', ({ transaction }) => { // The editor state has changed. } -editor.on('focus', () => { +editor.on('focus', ({ event }) => { // The editor is focused. } -editor.on('blur', () => { +editor.on('blur', ({ event }) => { // The editor isn’t focused anymore. } ``` diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index 16f9d8f9..59f30a38 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -18,7 +18,6 @@ import { EditorOptions, EditorContent, CommandSpec, - EditorSelection, } from './types' import * as extensions from './extensions' import style from './style' @@ -44,8 +43,6 @@ export class Editor extends EventEmitter { public view!: EditorView - public selection: EditorSelection = { from: 0, to: 0 } - public isFocused = false public options: EditorOptions = { @@ -61,6 +58,7 @@ export class Editor extends EventEmitter { enablePasteRules: true, onInit: () => null, onUpdate: () => null, + onSelection: () => null, onTransaction: () => null, onFocus: () => null, onBlur: () => null, @@ -83,6 +81,7 @@ export class Editor extends EventEmitter { this.injectCSS() this.on('init', this.options.onInit) this.on('update', this.options.onUpdate) + this.on('selection', this.options.onSelection) this.on('transaction', this.options.onTransaction) this.on('focus', this.options.onFocus) this.on('blur', this.options.onBlur) @@ -295,14 +294,6 @@ export class Editor extends EventEmitter { return this.createDocument('') } - /** - * Store the current selection. - */ - private storeSelection() { - const { from, to } = this.state.selection - this.selection = { from, to } - } - /** * The callback over which to send transactions (state updates) produced by the view. * @@ -310,11 +301,15 @@ export class Editor extends EventEmitter { */ private dispatchTransaction(transaction: Transaction) { const state = this.state.apply(transaction) + const selectionHasChanged = !this.state.selection.eq(state.selection) this.view.updateState(state) - this.storeSelection() this.emit('transaction', { transaction }) + if (selectionHasChanged) { + this.emit('selection') + } + const focus = transaction.getMeta('focus') const blur = transaction.getMeta('blur') diff --git a/packages/core/src/commands/focus.ts b/packages/core/src/commands/focus.ts index fb7f970e..10820c8d 100644 --- a/packages/core/src/commands/focus.ts +++ b/packages/core/src/commands/focus.ts @@ -42,7 +42,7 @@ export const focus = (position: FocusPosition = null): Command => ({ return true } - const { from, to } = resolveSelection(editor.state, position) || editor.selection + const { from, to } = resolveSelection(editor.state, position) || editor.state.selection const { doc } = tr const resolvedFrom = minMax(from, 0, doc.content.size) const resolvedEnd = minMax(to, 0, doc.content.size) diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index fececac3..cb400f61 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -27,16 +27,12 @@ export interface EditorOptions { enablePasteRules: boolean, onInit: () => void, onUpdate: () => void, + onSelection: () => void, onTransaction: (props: { transaction: Transaction }) => void, onFocus: (props: { event: FocusEvent }) => void, onBlur: (props: { event: FocusEvent }) => void, } -export type EditorSelection = { - from: number, - to: number, -} - export type EditorContent = string | JSON | null export type Command = (props: {