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 system. It is developed to power our online editing platform [Substance](http://substance.io).
58 lines (50 loc) • 1.24 kB
JavaScript
export default class CollectionItemLabelManager {
constructor (editorSession) {
this.editorSession = editorSession
this.initialize()
}
initialize () {
const path = this.getPath()
this.editorSession.getEditorState().addObserver(['document'], this._onChange, this, {
stage: 'update',
document: {
path
}
})
}
dispose () {
this.editorSession.getEditorState().removeObserver(this)
}
getPath () {
throw new Error('Not implemented.')
}
/**
* @param {*} item
*
* @example
* ```
* // simple counter
* String(item.getPosition() + 1)
* // other counters
* LATIN_LETTERS_UPPER_CASE.charAt(item.getPosition())
* // template string
* `Figure ${item.getPosition() + 1}`
* ```
*/
getItemLabel (item) {
throw new Error('Not implemented.')
}
update () {
const doc = this.editorSession.getDocument()
const path = this.getPath()
const items = doc.resolve(path, true)
const stateUpdates = items.map(item => {
const label = this.getItemLabel(item)
return [item.id, { label }]
})
this.editorSession.updateNodeStates(stateUpdates, { silent: true })
}
_onChange () {
this.update()
}
}