Skip to content

Instantly share code, notes, and snippets.

@tslocke
Created December 22, 2023 17:59
Show Gist options
  • Save tslocke/466271841ea57f911fdddd917f4d478e to your computer and use it in GitHub Desktop.
Save tslocke/466271841ea57f911fdddd917f4d478e to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script type="module">
import {Schema, Node} from 'https://unpkg.com/prosemirror-model?module'
import {EditorView} from 'https://unpkg.com/prosemirror-view?module'
import {EditorState} from 'https://unpkg.com/prosemirror-state?module'
const schema = new Schema({
nodes: {
button: { attrs: {label: {default: ''}} },
text: { content: 'text*' },
doc: { content: 'button+' },
}
})
function buttonView(node, view) {
const button = document.createElement('button')
button.textContent = node.attrs.label
button.onclick = changeBoth
return {
dom: button,
update(node) {
console.log('update', view.tag)
button.textContent = node.attrs.label
return true
}
}
}
const doc = schema.node('doc', null, [
schema.node('button', {label: 'One'}),
])
const state = EditorState.create({schema, doc})
const nodeViews = {button: buttonView}
const v1 = new EditorView(document.querySelector('#editor'), {state, nodeViews})
const v2 = new EditorView(null, {state, nodeViews})
v1.tag = 'mounted'
v2.tag = 'headless'
function changeBoth() {
changeLabel(v1)
changeLabel(v2)
}
function changeLabel(v) {
const tr = v.state.tr.setNodeAttribute(0, 'label', 'Changed')
v.dispatch(tr)
}
</script>
</head>
<body>
<div id="editor"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment