This repository has been archived by the owner on Dec 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
render.tsx
109 lines (97 loc) · 3.18 KB
/
render.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { useScopedDispatch, useScopedStore } from '@edtr-io/core'
import { StateTypeReturnType } from '@edtr-io/plugin'
import {
change,
findNextNode,
findPreviousNode,
getDocument,
getFocusTree
} from '@edtr-io/store'
import * as React from 'react'
import { rowsState, rowState } from '..'
export function RowRenderer({
row,
rows,
index,
renderIntoExtendedSettings,
PrimarySettingsWrapper
}: {
row: StateTypeReturnType<typeof rowState>
rows: StateTypeReturnType<typeof rowsState>
index: number
renderIntoExtendedSettings: (children: React.ReactChild) => React.ReactNode
PrimarySettingsWrapper: React.ComponentType
}) {
const dispatch = useScopedDispatch()
const store = useScopedStore()
const pluginProps = React.useMemo(() => {
return {
renderIntoExtendedSettings,
PrimarySettingsWrapper,
insert: (options?: { plugin: string; state?: unknown }) =>
rows.insert(index + 1, options),
replace: (options?: { plugin: string; state?: unknown }) => {
rows.remove(index)
rows.insert(index, options)
},
remove: () => {
rows.remove(index)
},
mergeWithPrevious: (merge: (statePrevious: unknown) => unknown) => {
if (index - 1 < 0) return
const current = getDocument(row.id)(store.getState())
let previous = getDocument(rows[index - 1].id)(store.getState())
if (!previous || !current) return
if (previous.plugin !== current.plugin) {
// check if previous focus plugin is the same type
const root = getFocusTree()(store.getState())
if (!root) return
const previousFocusId = findPreviousNode(root, row.id)
if (!previousFocusId) return
previous = getDocument(previousFocusId)(store.getState())
if (!previous || previous.plugin !== current.plugin) return
const merged = merge(previous.state)
dispatch(
change({
id: previousFocusId,
state: () => merged
})
)
rows.remove(index)
} else {
merge(previous.state)
setTimeout(() => rows.remove(index - 1))
}
},
mergeWithNext: (merge: (statePrevious: unknown) => unknown) => {
if (index + 1 === rows.length) return
const current = getDocument(row.id)(store.getState())
let next = getDocument(rows[index + 1].id)(store.getState())
if (!next || !current) return
if (next.plugin !== current.plugin) {
// check if next focus plugin is the same type
const root = getFocusTree()(store.getState())
if (!root) return
const nextFocusId = findNextNode(root, row.id)
if (!nextFocusId) return
// use that plugin for merge
next = getDocument(nextFocusId)(store.getState())
if (!next || next.plugin !== current.plugin) return
}
merge(next.state)
setTimeout(() => {
rows.remove(index + 1)
})
}
}
}, [
store,
PrimarySettingsWrapper,
dispatch,
index,
renderIntoExtendedSettings,
row.id,
rows
])
return <React.Fragment>{row.render(pluginProps)}</React.Fragment>
}