2022-09-09 11:53:35 +00:00
|
|
|
import { createContext, useContext } from 'solid-js'
|
|
|
|
import type { Store } from 'solid-js/store'
|
2022-10-09 00:00:13 +00:00
|
|
|
import type { XmlFragment } from 'yjs'
|
2022-09-09 11:53:35 +00:00
|
|
|
import type { WebrtcProvider } from 'y-webrtc'
|
2022-10-09 00:00:13 +00:00
|
|
|
import type { ProseMirrorExtension, ProseMirrorState } from '../prosemirror/helpers'
|
2022-10-07 12:57:33 +00:00
|
|
|
import type { EditorView } from 'prosemirror-view'
|
2022-10-09 00:00:13 +00:00
|
|
|
import { createEmptyText } from '../prosemirror/setup'
|
|
|
|
import type { Shout } from '../../../graphql/types.gen'
|
2022-09-09 11:53:35 +00:00
|
|
|
|
|
|
|
export interface Args {
|
2022-10-09 00:00:13 +00:00
|
|
|
draft: string // path to draft
|
2022-09-09 11:53:35 +00:00
|
|
|
cwd?: string
|
|
|
|
file?: string
|
|
|
|
room?: string
|
2022-10-07 19:35:53 +00:00
|
|
|
text?: string
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface PrettierConfig {
|
|
|
|
printWidth: number
|
|
|
|
tabWidth: number
|
|
|
|
useTabs: boolean
|
|
|
|
semi: boolean
|
|
|
|
singleQuote: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Config {
|
|
|
|
theme: string
|
|
|
|
// codeTheme: string;
|
|
|
|
font: string
|
|
|
|
fontSize: number
|
|
|
|
contentWidth: number
|
|
|
|
alwaysOnTop: boolean
|
|
|
|
// typewriterMode: boolean;
|
|
|
|
prettier: PrettierConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ErrorObject {
|
|
|
|
id: string
|
2022-10-09 00:00:13 +00:00
|
|
|
props?: unknown
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 00:00:13 +00:00
|
|
|
export interface YOptions {
|
|
|
|
type: XmlFragment
|
2022-09-09 11:53:35 +00:00
|
|
|
provider: WebrtcProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Collab {
|
|
|
|
started?: boolean
|
|
|
|
error?: boolean
|
|
|
|
room?: string
|
2022-10-09 00:00:13 +00:00
|
|
|
y?: YOptions
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export type LoadingType = 'loading' | 'initialized'
|
|
|
|
|
|
|
|
export interface State {
|
|
|
|
text?: ProseMirrorState
|
2022-10-07 12:57:33 +00:00
|
|
|
editorView?: EditorView
|
2022-09-09 11:53:35 +00:00
|
|
|
extensions?: ProseMirrorExtension[]
|
|
|
|
markdown?: boolean
|
|
|
|
lastModified?: Date
|
2022-10-09 00:00:13 +00:00
|
|
|
drafts: Draft[]
|
2022-09-09 11:53:35 +00:00
|
|
|
config: Config
|
|
|
|
error?: ErrorObject
|
|
|
|
loading: LoadingType
|
|
|
|
collab?: Collab
|
|
|
|
path?: string
|
|
|
|
args?: Args
|
|
|
|
}
|
|
|
|
|
2022-10-09 00:00:13 +00:00
|
|
|
export interface Draft {
|
|
|
|
extensions?: ProseMirrorExtension[]
|
|
|
|
updatedAt: Date
|
|
|
|
body?: string
|
|
|
|
text?: { doc: any; selection: { type: string; anchor: number; head: number } }
|
|
|
|
path?: string
|
|
|
|
markdown?: boolean
|
|
|
|
}
|
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
export class ServiceError extends Error {
|
|
|
|
public errorObject: ErrorObject
|
|
|
|
constructor(id: string, props: unknown) {
|
|
|
|
super(id)
|
2022-10-09 00:00:13 +00:00
|
|
|
this.errorObject = { id, props }
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 00:00:13 +00:00
|
|
|
export const StateContext = createContext<[Store<State>, any]>([undefined, undefined])
|
2022-09-09 11:53:35 +00:00
|
|
|
|
|
|
|
export const useState = () => useContext(StateContext)
|
|
|
|
|
|
|
|
export const newState = (props: Partial<State> = {}): State => ({
|
|
|
|
extensions: [],
|
2022-10-09 00:00:13 +00:00
|
|
|
drafts: [],
|
2022-09-09 11:53:35 +00:00
|
|
|
loading: 'loading',
|
|
|
|
markdown: false,
|
2022-10-09 00:00:13 +00:00
|
|
|
config: {
|
|
|
|
theme: undefined,
|
|
|
|
// codeTheme: 'material-light',
|
|
|
|
font: 'muller',
|
|
|
|
fontSize: 24,
|
|
|
|
contentWidth: 800,
|
|
|
|
alwaysOnTop: false,
|
|
|
|
// typewriterMode: true,
|
|
|
|
prettier: {
|
|
|
|
printWidth: 80,
|
|
|
|
tabWidth: 2,
|
|
|
|
useTabs: false,
|
|
|
|
semi: false,
|
|
|
|
singleQuote: true
|
|
|
|
}
|
|
|
|
},
|
2022-09-09 11:53:35 +00:00
|
|
|
...props
|
|
|
|
})
|
2022-10-09 00:00:13 +00:00
|
|
|
|
|
|
|
export const addToDrafts = (drafts: Draft[], state: State): Draft[] => {
|
|
|
|
drafts.forEach((d) => {
|
|
|
|
if (!state.drafts.includes(d)) state.drafts.push(d)
|
|
|
|
})
|
|
|
|
return state.drafts
|
|
|
|
}
|
|
|
|
|
|
|
|
export const createTextFromDraft = async (draft: Draft) => {
|
|
|
|
const created = createEmptyText()
|
|
|
|
created.doc.content = Object.values(draft.text) // FIXME
|
|
|
|
return created
|
|
|
|
}
|