MCPcopy
hub / github.com/marktext/marktext / drop

Method drop

packages/muya/src/editor/index.ts:253–355  ·  view source on GitHub ↗
(root: BlockNode, descent: JSONOpList)

Source from the content-addressed store, hash-verified

251 const snapshot = pick(this.scrollPage as BlockNode, operations);
252
253 function drop(root: BlockNode, descent: JSONOpList): BlockNode {
254 let subDoc = root;
255 let i = 0; // For reading
256 let m = 0;
257 const rootContainer: { root: BlockNode } = { root }; // This is an avoidable allocation.
258 let container: BlockNode | { root: BlockNode } = rootContainer;
259 let key: string | number = 'root'; // For writing
260
261 function mut() {
262 for (; m < i; m++) {
263 const d = descent[m];
264 if (typeof d === 'object')
265 continue;
266 if (key === 'root') {
267 const wrap = container as { root: BlockNode };
268 container = wrap.root;
269 }
270 else {
271 container = (container as BlockNode)?.queryBlock?.([key]);
272 }
273 key = d as string | number;
274 }
275 }
276
277 for (; i < descent.length; i++) {
278 const d = descent[i];
279
280 if (Array.isArray(d)) {
281 const child = drop(subDoc, d);
282 if (child !== subDoc && child !== undefined) {
283 mut();
284 // It maybe never go into this if statement.
285 if (key === 'root')
286 (container as { root: BlockNode }).root = child;
287 else
288 (container as Record<string, BlockNode>)[key] = child;
289 subDoc = child;
290 }
291 }
292 else if (typeof d === 'object') {
293 const comp = d as JSONOpComponent;
294 if (comp.i !== undefined) {
295 // Insert
296 mut();
297 const cur = container as BlockNode;
298 const ref = cur?.find?.(key);
299 if (typeof key === 'number') {
300 const insertedState = comp.i as { name: string };
301 const newBlock = ScrollPage.loadBlock(insertedState.name).create(muya, insertedState) as BlockNode;
302 if (cur && ref && newBlock)
303 cur.insertBefore?.(newBlock, ref, 'api');
304
305 subDoc = newBlock;
306 }
307 else {
308 switch (key) {
309 case 'checked': {
310 ref?.update?.(comp.i, 'api');

Callers

nothing calls this directly

Calls 7

loadBlockMethod · 0.80
applyMethod · 0.80
findMethod · 0.45
createMethod · 0.45
insertBeforeMethod · 0.45
updateMethod · 0.45
queryBlockMethod · 0.45

Tested by

no test coverage detected