Skip to content

Commit

Permalink
Merge pull request #210 from Chrissi2812/reactive-plugin-options
Browse files Browse the repository at this point in the history
Reactive extension options
  • Loading branch information
philippkuehn committed Mar 6, 2019
2 parents 575b082 + 1dbf542 commit bafa4a6
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -299,6 +299,7 @@ The most powerful feature of tiptap is that you can create your own extensions.
| `commands({ schema, attrs })` | `Object` | `null` | Define a command. |
| `inputRules({ schema })` | `Array` | `[]` | Define a list of input rules. |
| `pasteRules({ schema })` | `Array` | `[]` | Define a list of paste rules. |
| `get update()` | `Function` | `undefined` | Called when options of extension are changed via `editor.extensions.options` |

### Node|Mark Class

Expand Down
7 changes: 7 additions & 0 deletions examples/Components/Routes/Placeholder/index.vue
@@ -1,5 +1,6 @@
<template>
<div class="editor">
<input type="text" v-model="placeholder">
<editor-content class="editor__content" :editor="editor" />
</div>
</template>
Expand All @@ -18,6 +19,7 @@ export default {
},
data() {
return {
placeholder: 'Write something …',
editor: new Editor({
extensions: [
new BulletList(),
Expand All @@ -34,6 +36,11 @@ export default {
beforeDestroy() {
this.editor.destroy()
},
watch: {
placeholder(newValue) {
this.editor.extensions.options.placeholder.emptyNodeText = newValue
},
},
}
</script>

Expand Down
6 changes: 6 additions & 0 deletions packages/tiptap-extensions/src/extensions/Placeholder.js
Expand Up @@ -15,6 +15,12 @@ export default class Placeholder extends Extension {
}
}

get update() {
return view => {
view.updateState(view.state)
}
}

get plugins() {
return [
new Plugin({
Expand Down
3 changes: 3 additions & 0 deletions packages/tiptap/src/Editor.js
Expand Up @@ -67,6 +67,9 @@ export default class Editor {
view: this.view,
state: this.state,
})

// give extension manager access to our view
this.extensions.view = this.view
}

setOptions(options) {
Expand Down
4 changes: 4 additions & 0 deletions packages/tiptap/src/Utils/Extension.js
Expand Up @@ -15,6 +15,10 @@ export default class Extension {
return 'extension'
}

get update() {
return () => {}
}

get defaultOptions() {
return {}
}
Expand Down
21 changes: 21 additions & 0 deletions packages/tiptap/src/Utils/ExtensionManager.js
Expand Up @@ -15,6 +15,27 @@ export default class ExtensionManager {
}), {})
}

get options() {
const { view } = this
return this.extensions
.reduce((nodes, extension) => ({
...nodes,
[extension.name]: new Proxy(extension.options, {
set(obj, prop, value) {
const changed = (obj[prop] !== value)

Object.assign(obj, { [prop]: value })

if (changed) {
extension.update(view)
}

return true
},
}),
}), {})
}

get marks() {
return this.extensions
.filter(extension => extension.type === 'mark')
Expand Down

0 comments on commit bafa4a6

Please sign in to comment.