add resize event

This commit is contained in:
Philipp Kühn 2021-04-01 16:21:47 +02:00
parent b9ee5555e5
commit c68fa74ad1
3 changed files with 24 additions and 0 deletions

View File

@ -31,6 +31,9 @@ const editor = new Editor({
onBlur({ editor, event }) { onBlur({ editor, event }) {
// The editor isnt focused anymore. // The editor isnt focused anymore.
}, },
onResize({ editor, event }) {
// The editor view has resized.
},
onDestroy() { onDestroy() {
// The editor is being destroyed. // The editor is being destroyed.
}, },
@ -70,6 +73,10 @@ editor.on('blur', ({ editor, event }) => {
// The editor isnt focused anymore. // The editor isnt focused anymore.
} }
editor.on('resize', ({ editor, event }) => {
// The editor view has resized.
}
editor.on('destroy', () => { editor.on('destroy', () => {
// The editor is being destroyed. // The editor is being destroyed.
} }
@ -118,6 +125,9 @@ const CustomExtension = Extension.create({
onBlur({ editor, event }) { onBlur({ editor, event }) {
// The editor isnt focused anymore. // The editor isnt focused anymore.
}, },
onResize({ editor, event }) {
// The editor view has resized.
},
onDestroy() { onDestroy() {
// The editor is being destroyed. // The editor is being destroyed.
}, },

View File

@ -45,6 +45,8 @@ export class Editor extends EventEmitter {
public isFocused = false public isFocused = false
private resizeObserver!: ResizeObserver
public options: EditorOptions = { public options: EditorOptions = {
element: document.createElement('div'), element: document.createElement('div'),
content: '', content: '',
@ -63,6 +65,7 @@ export class Editor extends EventEmitter {
onTransaction: () => null, onTransaction: () => null,
onFocus: () => null, onFocus: () => null,
onBlur: () => null, onBlur: () => null,
onResize: () => null,
onDestroy: () => null, onDestroy: () => null,
} }
@ -86,7 +89,15 @@ export class Editor extends EventEmitter {
window.setTimeout(() => { window.setTimeout(() => {
this.commands.focus(this.options.autofocus) this.commands.focus(this.options.autofocus)
this.emit('create', { editor: this }) this.emit('create', { editor: this })
if (window.ResizeObserver) {
this.resizeObserver = new ResizeObserver(() => {
this.emit('resize', { editor: this })
})
this.resizeObserver.observe(this.view.dom)
}
}, 0) }, 0)
} }
/** /**
@ -442,6 +453,8 @@ export class Editor extends EventEmitter {
* Destroy the editor. * Destroy the editor.
*/ */
public destroy(): void { public destroy(): void {
this.resizeObserver?.unobserve(this.view.dom)
this.emit('destroy') this.emit('destroy')
if (this.view) { if (this.view) {

View File

@ -36,6 +36,7 @@ export interface EditorOptions {
onTransaction: (props: { editor: Editor, transaction: Transaction }) => void, onTransaction: (props: { editor: Editor, transaction: Transaction }) => void,
onFocus: (props: { editor: Editor, event: FocusEvent }) => void, onFocus: (props: { editor: Editor, event: FocusEvent }) => void,
onBlur: (props: { editor: Editor, event: FocusEvent }) => void, onBlur: (props: { editor: Editor, event: FocusEvent }) => void,
onResize: (props: { editor: Editor }) => void,
onDestroy: () => void, onDestroy: () => void,
} }