* 跳出代码块
(direction: 1 | -1)
| 1568 | * 跳出代码块 |
| 1569 | */ |
| 1570 | private exitCodeBlock(direction: 1 | -1): void { |
| 1571 | const pos = this.getPos(); |
| 1572 | if (pos === undefined) return; |
| 1573 | |
| 1574 | const { state } = this.view; |
| 1575 | const nodeEnd = pos + this.node.nodeSize; |
| 1576 | |
| 1577 | if (direction === 1) { |
| 1578 | const isLastNode = nodeEnd >= state.doc.content.size; |
| 1579 | |
| 1580 | if (isLastNode) { |
| 1581 | const paragraph = state.schema.nodes.paragraph.create(); |
| 1582 | const tr = state.tr.insert(nodeEnd, paragraph); |
| 1583 | tr.setSelection(TextSelection.create(tr.doc, nodeEnd + 1)); |
| 1584 | this.view.dispatch(tr); |
| 1585 | this.view.focus(); |
| 1586 | return; |
| 1587 | } |
| 1588 | |
| 1589 | const selection = Selection.near(state.doc.resolve(nodeEnd), 1); |
| 1590 | this.view.dispatch(state.tr.setSelection(selection)); |
| 1591 | this.view.focus(); |
| 1592 | } else { |
| 1593 | const selection = Selection.near(state.doc.resolve(pos), -1); |
| 1594 | // 如果找到的选区不在代码块之前,说明前方无可用位置,需创建段落 |
| 1595 | if (selection.from >= pos) { |
| 1596 | const paragraph = state.schema.nodes.paragraph.create(); |
| 1597 | const tr = state.tr.insert(pos, paragraph); |
| 1598 | tr.setSelection(TextSelection.create(tr.doc, pos + 1)); |
| 1599 | this.view.dispatch(tr); |
| 1600 | this.view.focus(); |
| 1601 | return; |
| 1602 | } |
| 1603 | this.view.dispatch(state.tr.setSelection(selection)); |
| 1604 | this.view.focus(); |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | /** |
| 1609 | * 跳出代码块并创建新的列表项 |
no test coverage detected