Skip to content

Commit

Permalink
wip: start of api
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Jan 25, 2024
1 parent b2b421d commit 7952481
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 7 deletions.
11 changes: 10 additions & 1 deletion packages/apps/backend/src/api-server/services/PartService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import EventEmitter from 'eventemitter3'
import { Application, PaginationParams, Params } from '@feathersjs/feathers'
import { ServiceTypes, Services, PartServiceDefinition as Definition } from '@sofie-prompter-editor/shared-model'
import {
ServiceTypes,
Services,
PartServiceDefinition as Definition,
ScriptContents,
} from '@sofie-prompter-editor/shared-model'
export { PlaylistServiceDefinition } from '@sofie-prompter-editor/shared-model'
import { PublishChannels } from '../PublishChannels.js'
import { CustomFeathersService } from './lib.js'
Expand Down Expand Up @@ -110,6 +115,10 @@ export class PartService extends EventEmitter<Definition.Events> implements Defi
public async remove(_id: NullId, _params?: Params): Promise<Result> {
throw new NotImplemented(`Not supported`)
}

public async updateScript(_id: Id, _scriptContents: ScriptContents): Promise<void> {
throw new NotImplemented(`Not implemented`)
}
}
type Result = Definition.Result
type Id = Definition.Id
Expand Down
14 changes: 12 additions & 2 deletions packages/apps/client/src/components/ScriptEditor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { EXTERNAL_STATE_CHANGE, updateModel } from './plugins/updateModel'
import { readOnlyNodeFilter } from './plugins/readOnlyNodeFilter'
import { formatingKeymap } from './keymaps'
import { deselectAll } from './commands/deselectAll'
import { fromMarkdown } from 'src/lib/prosemirrorDoc'
import { fromMarkdown, toMarkdown } from 'src/lib/prosemirrorDoc'
import { RootAppStore } from 'src/stores/RootAppStore'
import { IReactionDisposer, reaction } from 'mobx'

Expand Down Expand Up @@ -216,7 +216,17 @@ function makeNewEditorState(doc: Node): EditorState {
keymap(formatingKeymap),
keymap(baseKeymap),
readOnlyNodeFilter(),
updateModel((lineId, change) => console.log(lineId, change, doc)),
updateModel((lineId, lineNodes) => {
console.log(lineId, lineNodes)

const compiledMarkdown = toMarkdown(lineNodes)
console.log('markdown', compiledMarkdown)

const openRundown = RootAppStore.rundownStore.openRundown

// TODO - debounce? require manual execution?
if (openRundown) openRundown.updatePartScript(lineId, compiledMarkdown)
}),
],
doc,
})
Expand Down
79 changes: 78 additions & 1 deletion packages/apps/client/src/lib/prosemirrorDoc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Node as ProsemirrorNode } from 'prosemirror-model'
import { schema } from 'src/components/ScriptEditor/scriptSchema'
import { Node as MdAstNode } from './mdParser/astNodes'
import { Node as MdAstNode, RootNode as MdRootNode } from './mdParser/astNodes'
import createMdParser from './mdParser'
import { assertNever } from '@sofie-prompter-editor/shared-lib'

export function fromMarkdown(text: string | null): ProsemirrorNode[] {
if (text === null) return []
Expand Down Expand Up @@ -41,3 +42,79 @@ function traverseMdAstNodes(nodes: MdAstNode[]): ProsemirrorNode[] {

return result
}

export function toMarkdown(nodes: ProsemirrorNode[]): string {
if (nodes.length === 0) return ''

const mdAst: MdRootNode = {
type: 'root',
children: nodes.map(prosemirrorNodeToMarkdown),
}

return stringifyMarkdown(mdAst)
}

function stringifyMarkdown(mdAst: MdAstNode): string {
if (mdAst.type === 'root') {
return mdAst.children.map(stringifyMarkdown).join('')
} else if (mdAst.type === 'paragraph') {
return mdAst.children.map(stringifyMarkdown).join('') + '\n'
} else if (mdAst.type === 'text') {
return mdAst.value
} else if (mdAst.type === 'emphasis' || mdAst.type === 'strong' || mdAst.type === 'reverse') {
return `${mdAst.code}${mdAst.children.map(stringifyMarkdown).join('')}${mdAst.code}`
} else {
assertNever(mdAst)
console.warn(mdAst)
return '[UNKNOWN]'
}
}

function prosemirrorNodeToMarkdown(node: ProsemirrorNode): MdAstNode {
if (node.type === schema.nodes.paragraph) {
const children: MdAstNode[] = []
for (let i = 0; i < node.childCount; i++) {
children.push(prosemirrorNodeToMarkdown(node.child(i)))
}

return {
type: 'paragraph',
children: children,
}
} else if (node.type === schema.nodes.text) {
let textNode: MdAstNode = {
type: 'text',
value: node.text ?? '',
}

if (node.marks.find((mark) => mark.type === schema.marks.bold)) {
textNode = {
type: 'strong',
children: [textNode],
code: '**',
}
}
if (node.marks.find((mark) => mark.type === schema.marks.italic)) {
textNode = {
type: 'emphasis',
children: [textNode],
code: '__',
}
}
if (node.marks.find((mark) => mark.type === schema.marks.reverse)) {
textNode = {
type: 'reverse',
children: [textNode],
code: '~',
}
}

return textNode
} else {
console.warn(node)
return {
type: 'text',
value: '[UNKNOWN]',
}
}
}
11 changes: 10 additions & 1 deletion packages/apps/client/src/model/UIRundown.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { action, computed, makeAutoObservable, observable } from 'mobx'
import {
PartId,
Rundown,
RundownId,
RundownPlaylist,
RundownPlaylistId,
ScriptContents,
Segment,
SegmentId,
} from '@sofie-prompter-editor/shared-model'
Expand All @@ -29,7 +31,7 @@ export class UIRundown {
})
this.init().catch(console.error)
}
async init() {
private async init() {
await this.store.connection.rundown.subscribeToRundownsInPlaylist(this.id)

const rundowns = await this.store.connection.rundown.find({
Expand Down Expand Up @@ -145,4 +147,11 @@ export class UIRundown {
// update existing segment
existing.updateFromJson(json)
})

async updatePartScript(id: PartId, script: ScriptContents): Promise<void> {
// TODO - is this in the correct place?
await this.store.connection.part.updateScript(id, script)

// TODO - should this have any side effects?
}
}
5 changes: 3 additions & 2 deletions packages/shared/model/src/client-server-api/PartService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
assertConstIsValid,
assertConstIncludesAllMethods,
} from './lib.js'
import { Part } from '../model/index.js'
import { Part, ScriptContents } from '../model/index.js'
import { Diff } from '../patch.js'

/** List of all method names */
Expand All @@ -18,6 +18,7 @@ export const ALL_METHODS = [
// 'patch',
'remove',
//
'updateScript',
] as const
/** The methods exposed by this class are exposed in the API */
interface Methods extends Omit<ServiceMethods, 'patch'> {
Expand All @@ -34,7 +35,7 @@ interface Methods extends Omit<ServiceMethods, 'patch'> {

/** updates .isNew */
// setIsNew(read: boolean): Promise<void>
// updateScript(scriptContents?: ScriptContents): Promise<void>
updateScript(id: Id, scriptContents: ScriptContents): Promise<void>
}
export interface Service extends Methods, EventEmitter<Events> {}

Expand Down

0 comments on commit 7952481

Please sign in to comment.