()
| 3 | import { getAddMaterialOps } from './add-material' |
| 4 | |
| 5 | export default function insertNodeMiddleware(): Middleware { |
| 6 | return async (data, ctx, next) => { |
| 7 | if (data.type === OpType.INSERT_NODE) { |
| 8 | const toPos = data.preload.to |
| 9 | if (!toPos) { |
| 10 | return next() |
| 11 | } |
| 12 | |
| 13 | const { newText = '', wrap, material } = data.preload.data || {} |
| 14 | const line = ctx.lineContents.locateLineByPos(toPos.line) |
| 15 | let lineChars = line.toChars() |
| 16 | |
| 17 | const insertContent = async () => { |
| 18 | if (newText) { |
| 19 | lineChars.insert(toPos.column, newText) |
| 20 | } else if (material) { |
| 21 | const ops = await getAddMaterialOps(ctx.lineContents, toPos, material) |
| 22 | await ctx.runLoop(ops.insertDeps as any, ctx) |
| 23 | if (ops.insertCode) { |
| 24 | // @ts-ignore |
| 25 | lineChars.insert(toPos.column, ops.insertCode.preload.data?.newText) |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | if (wrap) { |
| 31 | const { startStr = '', endStr = '', anotherTo } = wrap |
| 32 | const anotherLine = ctx.lineContents.locateLineByPos(anotherTo.line) |
| 33 | const anotherChars = anotherLine === line ? lineChars : anotherLine.toChars() |
| 34 | |
| 35 | const flag = comparePoint(toPos, anotherTo) |
| 36 | if (flag > 0) { |
| 37 | lineChars.insert(toPos.column, endStr) |
| 38 | await insertContent() |
| 39 | anotherChars.insert(anotherTo.column, startStr) |
| 40 | } else { |
| 41 | anotherChars.insert(anotherTo.column, endStr) |
| 42 | await insertContent() |
| 43 | lineChars.insert(toPos.column, startStr) |
| 44 | } |
| 45 | anotherLine.setContent(anotherChars.toString()) |
| 46 | } else { |
| 47 | await insertContent() |
| 48 | } |
| 49 | |
| 50 | line.setContent(lineChars.toString()) |
| 51 | // line.setContent(line.slice(0, toPos.column).content + newText + line.slice(toPos.column ?? Infinity).content) |
| 52 | } |
| 53 | return next() |
| 54 | } |
| 55 | } |
no test coverage detected