Merge branch 'main' of github.com:ueberdosis/tiptap-next into main

This commit is contained in:
Hans Pagel
2020-11-27 15:23:25 +01:00
4 changed files with 18 additions and 20 deletions

View File

@@ -16,6 +16,9 @@ const editor = new Editor({
onUpdate() { onUpdate() {
// The content has changed. // The content has changed.
}, },
onSelection() {
// The selection has changed.
},
onTransaction({ transaction }) { onTransaction({ transaction }) {
// The editor state has changed. // The editor state has changed.
}, },
@@ -41,15 +44,19 @@ editor.on('update', () => {
// The content has changed. // The content has changed.
} }
editor.on('selection', () => {
// The selection has changed.
}
editor.on('transaction', ({ transaction }) => { editor.on('transaction', ({ transaction }) => {
// The editor state has changed. // The editor state has changed.
} }
editor.on('focus', () => { editor.on('focus', ({ event }) => {
// The editor is focused. // The editor is focused.
} }
editor.on('blur', () => { editor.on('blur', ({ event }) => {
// The editor isnt focused anymore. // The editor isnt focused anymore.
} }
``` ```

View File

@@ -18,7 +18,6 @@ import {
EditorOptions, EditorOptions,
EditorContent, EditorContent,
CommandSpec, CommandSpec,
EditorSelection,
} from './types' } from './types'
import * as extensions from './extensions' import * as extensions from './extensions'
import style from './style' import style from './style'
@@ -44,8 +43,6 @@ export class Editor extends EventEmitter {
public view!: EditorView public view!: EditorView
public selection: EditorSelection = { from: 0, to: 0 }
public isFocused = false public isFocused = false
public options: EditorOptions = { public options: EditorOptions = {
@@ -61,6 +58,7 @@ export class Editor extends EventEmitter {
enablePasteRules: true, enablePasteRules: true,
onInit: () => null, onInit: () => null,
onUpdate: () => null, onUpdate: () => null,
onSelection: () => null,
onTransaction: () => null, onTransaction: () => null,
onFocus: () => null, onFocus: () => null,
onBlur: () => null, onBlur: () => null,
@@ -83,6 +81,7 @@ export class Editor extends EventEmitter {
this.injectCSS() this.injectCSS()
this.on('init', this.options.onInit) this.on('init', this.options.onInit)
this.on('update', this.options.onUpdate) this.on('update', this.options.onUpdate)
this.on('selection', this.options.onSelection)
this.on('transaction', this.options.onTransaction) this.on('transaction', this.options.onTransaction)
this.on('focus', this.options.onFocus) this.on('focus', this.options.onFocus)
this.on('blur', this.options.onBlur) this.on('blur', this.options.onBlur)
@@ -295,14 +294,6 @@ export class Editor extends EventEmitter {
return this.createDocument('') 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. * 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) { private dispatchTransaction(transaction: Transaction) {
const state = this.state.apply(transaction) const state = this.state.apply(transaction)
const selectionHasChanged = !this.state.selection.eq(state.selection)
this.view.updateState(state) this.view.updateState(state)
this.storeSelection()
this.emit('transaction', { transaction }) this.emit('transaction', { transaction })
if (selectionHasChanged) {
this.emit('selection')
}
const focus = transaction.getMeta('focus') const focus = transaction.getMeta('focus')
const blur = transaction.getMeta('blur') const blur = transaction.getMeta('blur')

View File

@@ -42,7 +42,7 @@ export const focus = (position: FocusPosition = null): Command => ({
return true 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 { doc } = tr
const resolvedFrom = minMax(from, 0, doc.content.size) const resolvedFrom = minMax(from, 0, doc.content.size)
const resolvedEnd = minMax(to, 0, doc.content.size) const resolvedEnd = minMax(to, 0, doc.content.size)

View File

@@ -27,16 +27,12 @@ export interface EditorOptions {
enablePasteRules: boolean, enablePasteRules: boolean,
onInit: () => void, onInit: () => void,
onUpdate: () => void, onUpdate: () => void,
onSelection: () => void,
onTransaction: (props: { transaction: Transaction }) => void, onTransaction: (props: { transaction: Transaction }) => void,
onFocus: (props: { event: FocusEvent }) => void, onFocus: (props: { event: FocusEvent }) => void,
onBlur: (props: { event: FocusEvent }) => void, onBlur: (props: { event: FocusEvent }) => void,
} }
export type EditorSelection = {
from: number,
to: number,
}
export type EditorContent = string | JSON | null export type EditorContent = string | JSON | null
export type Command = (props: { export type Command = (props: {