UNPKG

substance

Version:

Substance is a JavaScript library for web-based content editing. It provides building blocks for realizing custom text editors and web-based publishing systems.

98 lines (77 loc) 2.21 kB
import { forEach, EventEmitter, without } from '../util' /* Manages highlights. Used by {@link ui/ScrollPane}. @class @param {model/Document} doc document instance @example ``` var contentHighlights = new Highlights(doc); ``` */ class Highlights extends EventEmitter { constructor(doc) { super() this.doc = doc this._highlights = {} } /** Get currently active highlights. @return {Object} Returns current highlights as a scoped object. */ get() { return this._highlights } /** Set highlights. @param {Object} scoped object describing highlights @example ```js highlights.set({ 'figures': ['figure-1', 'figure-3'] 'citations': ['citation-1', 'citation-5'] }); ``` */ set(highlights) { let oldHighlights = this._highlights let doc = this.doc // Iterate over scopes of provided highlights forEach(highlights, function(newScopedHighlights, scope) { let oldScopedHighlights = oldHighlights[scope] || [] // old [1,2,3] -> new [2,4,5] // toBeDeleted: [1,3] // toBeAdded: [4,5] let toBeDeleted = without(oldScopedHighlights, newScopedHighlights) let toBeAdded = without(newScopedHighlights, oldScopedHighlights) // if (scopedHighlights) { forEach(toBeDeleted, function(nodeId) { let node = doc.get(nodeId) // Node could have been deleted in the meanwhile if (node) { if (node.setHighlighted) { node.setHighlighted(false, scope) } else { console.warn('setHighlighted is not defined on target node') } } }) forEach(toBeAdded, function(nodeId) { let node = doc.get(nodeId) if (node) { if (node.setHighlighted) { node.setHighlighted(true, scope) } else { console.warn('setHighlighted is not defined on target node') } } }) }) this._highlights = highlights /** Emitted when highlights have been updated @event ui/Highlights@highlights:updated */ this.emit('highlights:updated', highlights) } } export default Highlights