feat: add selection function

This commit is contained in:
DiamondThree
2022-08-09 18:41:08 +08:00
parent 75083f1c75
commit 36ad39237b

View File

@@ -30,6 +30,7 @@ import {
} from './types'; } from './types';
import { isLikeBlockListIds } from './utils'; import { isLikeBlockListIds } from './utils';
import { Protocol } from '@toeverything/datasource/db-service'; import { Protocol } from '@toeverything/datasource/db-service';
import { Editor } from 'slate';
// IMP: maybe merge active and select into single function // IMP: maybe merge active and select into single function
export type SelectionInfo = InstanceType< export type SelectionInfo = InstanceType<
@@ -321,16 +322,16 @@ export class SelectionManager implements VirgoSelection {
if (selectionRect.isIntersect(domToRect(block.dom))) { if (selectionRect.isIntersect(domToRect(block.dom))) {
const childrenBlocks = await block.children(); const childrenBlocks = await block.children();
// should check directly in structured block // should check directly in structured block
const structuredChildrenBlocks: Array<AsyncBlock> = const structuredChildrenBlocks: Array<AsyncBlock> = childrenBlocks.filter(
childrenBlocks.filter(childBlock => { childBlock => {
return this._editor.getView(childBlock.type).layoutOnly; return this._editor.getView(childBlock.type).layoutOnly;
}); }
);
for await (const childBlock of structuredChildrenBlocks) { for await (const childBlock of structuredChildrenBlocks) {
const childSelectedNodes = const childSelectedNodes = await this.calcRenderBlockIntersect(
await this.calcRenderBlockIntersect( selectionRect,
selectionRect, childBlock
childBlock );
);
selectedNodes.push(...childSelectedNodes); selectedNodes.push(...childSelectedNodes);
} }
const selectableChildren = childrenBlocks.filter(childBlock => { const selectableChildren = childrenBlocks.filter(childBlock => {
@@ -347,11 +348,10 @@ export class SelectionManager implements VirgoSelection {
} }
// if just only has one selected maybe select the children // if just only has one selected maybe select the children
if (selectedNodes.length === 1) { if (selectedNodes.length === 1) {
const childrenSelectedNodes: Array<AsyncBlock> = const childrenSelectedNodes: Array<AsyncBlock> = await this.calcRenderBlockIntersect(
await this.calcRenderBlockIntersect( selectionRect,
selectionRect, selectedNodes[0]
selectedNodes[0] );
);
if (childrenSelectedNodes.length) if (childrenSelectedNodes.length)
return childrenSelectedNodes; return childrenSelectedNodes;
} }
@@ -584,8 +584,7 @@ export class SelectionManager implements VirgoSelection {
} else { } else {
const new_node = await this._editor.getBlockById(newNodeId); const new_node = await this._editor.getBlockById(newNodeId);
if (new_node) { if (new_node) {
const new_node_children_ids = const new_node_children_ids = await new_node.childrenIds;
await new_node.childrenIds;
let select_ids_new = this._selectedNodesIds; let select_ids_new = this._selectedNodesIds;
if ( if (
new_node_children_ids && new_node_children_ids &&
@@ -1050,4 +1049,24 @@ export class SelectionManager implements VirgoSelection {
this._windowSelectionChangeHandler this._windowSelectionChangeHandler
); );
} }
/**
*
* move active selection to the new position
* @param index:number
* @memberof SelectionManager
*/
public moveCursor(index: number, blockId: string): void {
const now_range = window.getSelection().getRangeAt(0);
let preRang = document.createRange();
preRang.setStart(
now_range.startContainer,
now_range.startOffset + index
);
preRang.setEnd(now_range.endContainer, now_range.endOffset + index);
let prePosition = preRang.getClientRects().item(0);
this.activeNodeByNodeId(
blockId,
new Point(prePosition.left, prePosition.bottom)
);
}
} }