debug editor
This commit is contained in:
parent
19027ae643
commit
fed39bde86
|
@ -135,52 +135,45 @@ export const createCtrl = (initial): [Store<State>, { [key: string]: any }] => {
|
||||||
const readStoredState = async (): Promise<State> => {
|
const readStoredState = async (): Promise<State> => {
|
||||||
const state: State = unwrap(store)
|
const state: State = unwrap(store)
|
||||||
const room = window.location.pathname?.slice(1).trim()
|
const room = window.location.pathname?.slice(1).trim()
|
||||||
const args = { room, draft: room }
|
const args = { draft: room }
|
||||||
const data = await db.get('state')
|
const data = await db.get('state')
|
||||||
let parsed
|
|
||||||
if (data !== undefined) {
|
if (data !== undefined) {
|
||||||
try {
|
try {
|
||||||
parsed = JSON.parse(data)
|
const parsed = JSON.parse(data)
|
||||||
|
let text = state.text
|
||||||
|
if (parsed.text) {
|
||||||
|
if (!isText(parsed.text)) {
|
||||||
|
throw new ServiceError('invalid_state', parsed.text)
|
||||||
|
}
|
||||||
|
text = parsed.text
|
||||||
|
}
|
||||||
|
|
||||||
|
const extensions = createExtensions({
|
||||||
|
path: parsed.path,
|
||||||
|
markdown: parsed.markdown,
|
||||||
|
keymap,
|
||||||
|
config: undefined
|
||||||
|
})
|
||||||
|
|
||||||
|
for (const draft of parsed.drafts || []) {
|
||||||
|
if (!isDraft(draft)) {
|
||||||
|
console.error('[editor] invalid draft', draft)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...parsed,
|
||||||
|
text,
|
||||||
|
extensions,
|
||||||
|
// config,
|
||||||
|
args,
|
||||||
|
lastModified: new Date(parsed.lastModified)
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
throw new ServiceError('invalid_state', data)
|
return { ...state, args }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!parsed) {
|
|
||||||
return { ...state, args }
|
|
||||||
}
|
|
||||||
|
|
||||||
let text = state.text
|
|
||||||
if (parsed.text) {
|
|
||||||
if (!isText(parsed.text)) {
|
|
||||||
throw new ServiceError('invalid_state', parsed.text)
|
|
||||||
}
|
|
||||||
|
|
||||||
text = parsed.text
|
|
||||||
}
|
|
||||||
|
|
||||||
const extensions = createExtensions({
|
|
||||||
path: parsed.path,
|
|
||||||
markdown: parsed.markdown,
|
|
||||||
keymap,
|
|
||||||
config: undefined
|
|
||||||
})
|
|
||||||
|
|
||||||
for (const draft of parsed.drafts || []) {
|
|
||||||
if (!isDraft(draft)) {
|
|
||||||
throw new ServiceError('invalid_draft', draft)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
...parsed,
|
|
||||||
text,
|
|
||||||
extensions,
|
|
||||||
// config,
|
|
||||||
args,
|
|
||||||
lastModified: new Date(parsed.lastModified)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const getTheme = (state: State) => ({ theme: state.config.theme })
|
const getTheme = (state: State) => ({ theme: state.config.theme })
|
||||||
|
@ -209,6 +202,7 @@ export const createCtrl = (initial): [Store<State>, { [key: string]: any }] => {
|
||||||
|
|
||||||
const init = async () => {
|
const init = async () => {
|
||||||
let state = await readStoredState()
|
let state = await readStoredState()
|
||||||
|
console.log('[editor] init with state', state)
|
||||||
try {
|
try {
|
||||||
if (state.args.room) {
|
if (state.args.room) {
|
||||||
state = await doStartCollab(state)
|
state = await doStartCollab(state)
|
||||||
|
@ -241,6 +235,7 @@ export const createCtrl = (initial): [Store<State>, { [key: string]: any }] => {
|
||||||
config: { ...state.config, ...getTheme(state) },
|
config: { ...state.config, ...getTheme(state) },
|
||||||
loading: 'initialized'
|
loading: 'initialized'
|
||||||
})
|
})
|
||||||
|
console.log('[editor] initialized successfully', state)
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadDraft = async (config: Config, path: string): Promise<Draft> => {
|
const loadDraft = async (config: Config, path: string): Promise<Draft> => {
|
||||||
|
@ -361,6 +356,7 @@ export const createCtrl = (initial): [Store<State>, { [key: string]: any }] => {
|
||||||
const doStartCollab = async (state: State): Promise<State> => {
|
const doStartCollab = async (state: State): Promise<State> => {
|
||||||
const backup = state.args?.room && state.collab?.room !== state.args.room
|
const backup = state.args?.room && state.collab?.room !== state.args.room
|
||||||
const room = state.args?.room ?? uuidv4()
|
const room = state.args?.room ?? uuidv4()
|
||||||
|
state.args.room = room
|
||||||
window.history.replaceState(null, '', `/${room}`)
|
window.history.replaceState(null, '', `/${room}`)
|
||||||
const { roomConnect } = await import('../prosemirror/p2p')
|
const { roomConnect } = await import('../prosemirror/p2p')
|
||||||
const [type, provider] = roomConnect(room)
|
const [type, provider] = roomConnect(room)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user