MCPcopy Create free account
hub / github.com/editablejs/editable / wrapList

Function wrapList

packages/models/src/interfaces/list.ts:195–279  ·  view source on GitHub ↗
(
    editor: Editor,
    list: Partial<Omit<T, 'children'>> & { type: string },
    opitons: WrapListOptions = {},
  )

Source from the content-addressed store, hash-verified

193 },
194
195 wrapList<T extends List>(
196 editor: Editor,
197 list: Partial<Omit<T, 'children'>> & { type: string },
198 opitons: WrapListOptions = {},
199 ) {
200 const { at } = opitons
201 let { start = 1, template, type } = list
202 List.unwrapList(editor, {
203 at,
204 })
205 editor.normalizeSelection(selection => {
206 if (editor.selection !== selection) editor.selection = selection
207 if (!selection) return
208 const entrys = Editor.nodes<Element>(editor, {
209 at: selection,
210 match: n => Editor.isBlock(editor, n),
211 mode: 'lowest',
212 })
213
214 const beforePath = Editor.before(editor, selection.anchor.path)
215 const afterPath = Editor.after(editor, selection.focus.path)
216 const [prev] = Editor.nodes<List>(editor, {
217 at: beforePath,
218 match: n => Editor.isList(editor, n) && n.type === type,
219 })
220 let key = ''
221 let next: NodeEntry<List> | undefined = undefined
222 if (prev) {
223 const prevList = prev[0]
224 key = prevList.key
225 start = prevList.start + 1
226 } else if (
227 ([next] = Editor.nodes<List>(editor, {
228 at: afterPath,
229 match: n => Editor.isList(editor, n) && n.type === type,
230 })) &&
231 next
232 ) {
233 const nextList = next[0]
234 key = nextList.key
235 start = Math.max(nextList.start - 1, 1)
236 } else {
237 key = generateRandomKey()
238 }
239 const { props } = opitons
240
241 let prevPath = null
242 for (const [node, path] of entrys) {
243 if (prevPath) {
244 const prevNode = Node.get(editor, prevPath)
245 if (!Editor.isList(editor, prevNode)) {
246 start--
247 }
248 }
249 const newLevel = List.getLevel(editor, {
250 type,
251 path,
252 key,

Callers 3

index.tsFile · 0.85
index.tsFile · 0.85
index.tsFile · 0.85

Calls 3

generateRandomKeyFunction · 0.90
getMethod · 0.80
propsFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…