Skip to content

Commit

Permalink
chore(ScriptEditor): rename nodes in Schema to match the rest of the UI
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarpl committed Nov 21, 2023
1 parent 6403f77 commit e8641ed
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
29 changes: 10 additions & 19 deletions packages/apps/client/src/ScriptEditor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,15 @@ export function Editor({
void initialValue

useEffect(() => {
function onKeyDown(ev: KeyboardEvent) {
console.log(ev)
if (ev.key === 'w' && ev.ctrlKey) {
ev.preventDefault()
}
}

function onBeforeUnload(ev: BeforeUnloadEvent) {
ev.stopPropagation()
ev.preventDefault()
return false
}

window.addEventListener('keydown', onKeyDown)
window.addEventListener('beforeunload', onBeforeUnload, { capture: true })

return () => {
window.removeEventListener('keydown', onKeyDown)
window.removeEventListener('beforeunload', onBeforeUnload, { capture: true })
}
}, [])
Expand All @@ -57,32 +48,32 @@ export function Editor({
schema.node(schema.nodes.segment, undefined, [
schema.node(schema.nodes.segmentTitle, undefined, schema.text('Segment Title')),
schema.node(
schema.nodes.part,
schema.nodes.line,
{
partId: randomId(),
lineId: randomId(),
},
[
schema.node(schema.nodes.partTitle, undefined, schema.text('Part title')),
schema.node(schema.nodes.lineTitle, undefined, schema.text('Line title')),
schema.node(schema.nodes.paragraph, undefined, schema.text('Script...')),
]
),
schema.node(
schema.nodes.part,
schema.nodes.line,
{
partId: randomId(),
lineId: randomId(),
},
[
schema.node(schema.nodes.partTitle, undefined, schema.text('Part title')),
schema.node(schema.nodes.lineTitle, undefined, schema.text('Line title')),
schema.node(schema.nodes.paragraph, undefined, schema.text('Script...')),
]
),
schema.node(
schema.nodes.part,
schema.nodes.line,
{
partId: randomId(),
lineId: randomId(),
},
[
schema.node(schema.nodes.partTitle, undefined, schema.text('Part title')),
schema.node(schema.nodes.lineTitle, undefined, schema.text('Line title')),
schema.node(schema.nodes.paragraph, undefined, schema.text('Script...')),
]
),
Expand All @@ -98,7 +89,7 @@ export function Editor({
keymap(formatingKeymap),
keymap(baseKeymap),
readOnlyNodeFilter(),
updateModel(),
updateModel(console.log),
],
doc,
})
Expand Down
31 changes: 28 additions & 3 deletions packages/apps/client/src/ScriptEditor/plugins/updateModel.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import { Plugin } from 'prosemirror-state'
import { UILineId } from '../../model/UILine'

export function updateModel() {
export function updateModel(onChange?: (lineId: UILineId, contents: SomeContents) => void) {
return new Plugin({
appendTransaction: () => {
// console.log(newState)
appendTransaction: (trs, oldState, newState) => {
const anyChanges = trs.reduce<boolean>((memo, tr) => memo || tr.docChanged, false)
if (!anyChanges) return null

for (const tr of trs) {
for (const step of tr.steps) {
step.getMap().forEach((oldStart, oldEnd, newStart, newEnd) => {
oldState.doc.nodesBetween(oldStart, oldEnd, (_node, _pos, parent) => {
if (!parent) return
if (parent.type.name !== 'line') return

const lineId = parent.attrs['lineId'] as UILineId

newState.doc.nodesBetween(newStart, newEnd, (_node, _pos, parent) => {
if (!parent) return
if (parent.type.name !== 'line') return

if (onChange) onChange(lineId, parent.toString())
})
})
})
}
}

return null
},
})
}

type SomeContents = unknown
22 changes: 12 additions & 10 deletions packages/apps/client/src/ScriptEditor/scriptSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const schema = new Schema({

paragraph: nodes.paragraph,

partTitle: {
lineTitle: {
group: 'title',
content: 'text*',
atom: true,
Expand All @@ -17,20 +17,22 @@ export const schema = new Schema({
selectable: false,
locked: true,
toDOM() {
return ['h2', { class: 'PartSlug', contenteditable: 'false' }, 0]
return ['h3', { class: 'LineSlug', contenteditable: 'false' }, 0]
},
},
part: {
line: {
group: 'block',
content: 'partTitle paragraph*',
partId: {
default: null,
content: 'lineTitle paragraph*',
attrs: {
lineId: {
default: null,
},
},
isolating: true,
draggable: false,
selectable: false,
toDOM() {
return ['div', { class: 'part' }, 0]
return ['div', { class: 'Line' }, 0]
},
},

Expand All @@ -49,12 +51,12 @@ export const schema = new Schema({
},
segment: {
group: 'block',
content: 'segmentTitle part*',
content: 'segmentTitle line*',
isolating: true,
draggable: false,
selectable: false,
toDOM() {
return ['div', { class: 'segment' }, 0]
return ['div', { class: 'Segment' }, 0]
},
},

Expand All @@ -78,7 +80,7 @@ export const schema = new Schema({
draggable: false,
selectable: false,
toDOM() {
return ['div', { class: 'rundown' }, 0]
return ['div', { class: 'Rundown' }, 0]
},
},

Expand Down

0 comments on commit e8641ed

Please sign in to comment.