Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

feat: repair paste cursor

SaikaSakura322fb098a428
diff --git a/libs/components/common/src/lib/text/slate-utils.ts b/libs/components/common/src/lib/text/slate-utils.ts
index 3f5fb57697..1783d79cab 100644
--- a/libs/components/common/src/lib/text/slate-utils.ts
+++ b/libs/components/common/src/lib/text/slate-utils.ts
@@ -905,6 +905,19 @@ class SlateUtils {
);
}
+ public insertNodes(
+ nodes: SlateNode | Array<SlateNode>,
+ options?: Parameters<typeof Transforms.insertNodes>[2]
+ ) {
+ Transforms.insertNodes(this.editor, nodes, {
+ ...options,
+ });
+ }
+
+ public getNodeByPath(path: Path) {
+ Editor.node(this.editor, path);
+ }
+
public getStartSelection() {
return {
anchor: this.getStart(),
diff --git a/libs/components/editor-core/src/editor/block/block-helper.ts b/libs/components/editor-core/src/editor/block/block-helper.ts
index 1c1ab37dbe..7bd0284ffc 100644
--- a/libs/components/editor-core/src/editor/block/block-helper.ts
+++ b/libs/components/editor-core/src/editor/block/block-helper.ts
@@ -3,7 +3,13 @@ import type {
SlateUtils,
TextAlignOptions,
} from '@toeverything/components/common';
-import { Point, Selection as SlateSelection } from 'slate';
+import {
+ BaseRange,
+ Node,
+ Path,
+ Point,
+ Selection as SlateSelection,
+} from 'slate';
import { Editor } from '../editor';
type TextUtilsFunctions =
@@ -28,7 +34,10 @@ type TextUtilsFunctions =
| 'removeSelection'
| 'insertReference'
| 'isCollapsed'
- | 'blur';
+ | 'blur'
+ | 'setSelection'
+ | 'insertNodes'
+ | 'getNodeByPath';
type ExtendedTextUtils = SlateUtils & {
setLinkModalVisible: (visible: boolean) => void;
@@ -193,6 +202,59 @@ export class BlockHelper {
return '';
}
+ /**
+ *
+ * set selection of a text input
+ * @param {string} blockId
+ * @param {BaseRange} selection
+ * @return {*}
+ * @memberof BlockHelper
+ */
+ public setSelection(blockId: string, selection: BaseRange) {
+ const text_utils = this._blockTextUtilsMap[blockId];
+ if (text_utils) {
+ return text_utils.setSelection(selection);
+ }
+ console.warn('Could find the block text utils');
+ }
+
+ /**
+ *
+ * insert nodes in text
+ * @param {string} blockId
+ * @param {Array<Node>} nodes
+ * @param {Parameters<TextUtils['insertNodes']>[1]} options
+ * @return {*}
+ * @memberof BlockHelper
+ */
+ public insertNodes(
+ blockId: string,
+ nodes: Array<Node>,
+ options?: Parameters<TextUtils['insertNodes']>[1]
+ ) {
+ const text_utils = this._blockTextUtilsMap[blockId];
+ if (text_utils) {
+ return text_utils.insertNodes(nodes, options);
+ }
+ console.warn('Could find the block text utils');
+ }
+
+ /**
+ *
+ * get text(slate node) by path
+ * @param {string} blockId
+ * @param {Path} path
+ * @return {*}
+ * @memberof BlockHelper
+ */
+ public getNodeByPath(blockId: string, path: Path) {
+ const text_utils = this._blockTextUtilsMap[blockId];
+ if (text_utils) {
+ return text_utils.getNodeByPath(path);
+ }
+ console.warn('Could find the block text utils');
+ }
+
public transformPoint(
blockId: string,
...restArgs: Parameters<TextUtils['transformPoint']>
diff --git a/libs/components/editor-core/src/editor/clipboard/paste.ts b/libs/components/editor-core/src/editor/clipboard/paste.ts
index 022b3de214..6633b618e4 100644
--- a/libs/components/editor-core/src/editor/clipboard/paste.ts
+++ b/libs/components/editor-core/src/editor/clipboard/paste.ts
@@ -1,4 +1,4 @@
-import { HooksRunner } from '../types';
+/* eslint-disable max-lines */
import {
OFFICE_CLIPBOARD_MIMETYPE,
InnerClipInfo,
@@ -187,10 +187,15 @@ export class Paste {
const pureText = !shouldSplitBlock
? blocks[0].properties.text.value
: [{ text: '' }];
- this._editor.blockHelper.setBlockBlur(
- currentSelectInfo.blocks[0].blockId
+ this._editor.blockHelper.insertNodes(
+ selectedBlock.id,
+ pureText,
+ { select: true }
);
+ return;
+ //TODO repair the following logics
+ /**
const { startInfo, endInfo } = currentSelectInfo.blocks[0];
// 选中的当前的可编辑block的文字信息
@@ -414,13 +419,13 @@ export class Paste {
},
});
- const pastedTextLength = pureText.reduce(
- (sumLength: number, textItem: TextValueItem) => {
- sumLength += textItem.text.length;
- return sumLength;
- },
- 0
- );
+ // const pastedTextLength = pureText.reduce(
+ // (sumLength: number, textItem: TextValueItem) => {
+ // sumLength += textItem.text.length;
+ // return sumLength;
+ // },
+ // 0
+ // );
// this._editor.selectionManager.moveCursor(
// window.getSelection().getRangeAt(0),
@@ -429,6 +434,7 @@ export class Paste {
// );
}
}
+ */
} else {
const pasteBlocks = await this._createBlocks(blocks);
pasteBlocks.forEach(block => {
diff --git a/libs/components/editor-core/src/editor/clipboard/utils.ts b/libs/components/editor-core/src/editor/clipboard/utils.ts
index 7b4d420a3a..cb5d10241e 100644
--- a/libs/components/editor-core/src/editor/clipboard/utils.ts
+++ b/libs/components/editor-core/src/editor/clipboard/utils.ts
@@ -1,4 +1,4 @@
-import {Editor} from "../editor";
+import { Editor } from '../editor';
export const shouldHandlerContinue = (event: Event, editor: Editor) => {
const filterNodes = ['INPUT', 'SELECT', 'TEXTAREA'];
diff --git a/libs/components/editor-core/src/editor/selection/selection.ts b/libs/components/editor-core/src/editor/selection/selection.ts
index 99f648a930..40c069b941 100644
--- a/libs/components/editor-core/src/editor/selection/selection.ts
+++ b/libs/components/editor-core/src/editor/selection/selection.ts
@@ -322,16 +322,16 @@ export class SelectionManager implements VirgoSelection {
if (selectionRect.isIntersect(domToRect(block.dom))) {
const childrenBlocks = await block.children();
// should check directly in structured block
- const structuredChildrenBlocks: Array<AsyncBlock> = childrenBlocks.filter(
- childBlock => {
+ const structuredChildrenBlocks: Array<AsyncBlock> =
+ childrenBlocks.filter(childBlock => {
return this._editor.getView(childBlock.type).layoutOnly;
- }
- );
+ });
for await (const childBlock of structuredChildrenBlocks) {
- const childSelectedNodes = await this.calcRenderBlockIntersect(
- selectionRect,
- childBlock
- );
+ const childSelectedNodes =
+ await this.calcRenderBlockIntersect(
+ selectionRect,
+ childBlock
+ );
selectedNodes.push(...childSelectedNodes);
}
const selectableChildren = childrenBlocks.filter(childBlock => {
@@ -348,10 +348,11 @@ export class SelectionManager implements VirgoSelection {
}
// if just only has one selected maybe select the children
if (selectedNodes.length === 1) {
- const childrenSelectedNodes: Array<AsyncBlock> = await this.calcRenderBlockIntersect(
- selectionRect,
- selectedNodes[0]
- );
+ const childrenSelectedNodes: Array<AsyncBlock> =
+ await this.calcRenderBlockIntersect(
+ selectionRect,
+ selectedNodes[0]
+ );
if (childrenSelectedNodes.length)
return childrenSelectedNodes;
}
@@ -584,7 +585,8 @@ export class SelectionManager implements VirgoSelection {
} else {
const new_node = await this._editor.getBlockById(newNodeId);
if (new_node) {
- const new_node_children_ids = await new_node.childrenIds;
+ const new_node_children_ids =
+ await new_node.childrenIds;
let select_ids_new = this._selectedNodesIds;
if (
new_node_children_ids &&