Skip to content

Commit

Permalink
Add PluginValue.docViewUpdate
Browse files Browse the repository at this point in the history
FEATURE: View plugins can now provide a `docViewUpdate` method that
is called whenever the document view is updated.
  • Loading branch information
marijnh committed Mar 4, 2024
1 parent 4e355ea commit 524ef55
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/docview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {getAttrs} from "./attributes"
import {clientRectsFor, isEquivalentPosition, maxOffset, Rect, scrollRectIntoView,
getSelection, hasSelection, textRange, DOMSelectionState} from "./dom"
import {ViewUpdate, decorations as decorationsFacet, outerDecorations,
ChangedRange, ScrollTarget, getScrollMargins} from "./extension"
ChangedRange, ScrollTarget, getScrollMargins, requestMeasureOnDocUpdate} from "./extension"
import {EditorView} from "./editorview"
import {Direction} from "./bidi"

Expand Down
12 changes: 12 additions & 0 deletions src/editorview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ export class EditorView {
this.viewState.mustMeasureContent = true
if (redrawn || attrsChanged || scrollTarget || this.viewState.mustEnforceCursorAssoc || this.viewState.mustMeasureContent)
this.requestMeasure()
if (redrawn) this.docViewUpdate()
if (!update.empty) for (let listener of this.state.facet(updateListener)) {
try { listener(update) }
catch (e) { logException(this.state, e, "update listener") }
Expand Down Expand Up @@ -391,6 +392,16 @@ export class EditorView {
if (prevSpecs != specs) this.inputState.ensureHandlers(this.plugins)
}

private docViewUpdate() {
for (let plugin of this.plugins) {
let val = plugin.value
if (val && val.docViewUpdate) {
try { val.docViewUpdate(this) }
catch(e) { logException(this.state, e, "doc view update listener") }
}
}
}

/// @internal
measure(flush = true) {
if (this.destroyed) return
Expand Down Expand Up @@ -449,6 +460,7 @@ export class EditorView {
this.inputState.update(update)
this.updateAttrs()
redrawn = this.docView.update(update)
if (redrawn) this.docViewUpdate()
}
for (let i = 0; i < measuring.length; i++) if (measured[i] != BadMeasure) {
try {
Expand Down
6 changes: 6 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ export interface PluginValue extends Object {
/// your code in a DOM reading phase if you need to.
update?(update: ViewUpdate): void

/// Called when the document view is updated (due to content,
/// decoration, or viewport changes). Should not try to immediately
/// start another view update. Often useful for calling
/// [`requestMeasure`](#view.EditorView.requestMeasure).
docViewUpdate?(view: EditorView): void

/// Called when the plugin is no longer going to be used. Should
/// revert any changes the plugin made to the DOM.
destroy?(): void
Expand Down

0 comments on commit 524ef55

Please sign in to comment.