refactor: 优化画布锁机制

This commit is contained in:
lihao.ylh 2021-08-05 16:36:23 +08:00
parent b94b06965e
commit c6446d87dc
7 changed files with 17 additions and 24 deletions

View File

@ -112,7 +112,8 @@ export class BorderDetecting extends Component<{ host: BuiltinSimulatorHost }> {
} }
const lockedNode = getClosestNode(current, (n) => { const lockedNode = getClosestNode(current, (n) => {
return n?.getExtraProp('isLocked')?.getValue() === true; // 假如当前节点就是 locked 状态,要从当前节点的父节点开始查找
return !!(current?.isLocked ? n.parent?.isLocked : n.isLocked);
}); });
if (lockedNode && lockedNode.getId() !== current.getId()) { if (lockedNode && lockedNode.getId() !== current.getId()) {
// 选中父节锁定的节点 // 选中父节锁定的节点

View File

@ -135,7 +135,7 @@ function createAction(content: ReactNode | ComponentType<any> | ActionContentObj
className="lc-borders-action" className="lc-borders-action"
onClick={() => { onClick={() => {
action && action(node); action && action(node);
const editor = globalContext.get(Editor); const editor = globalContext.get('editor');
const npm = node?.componentMeta?.npm; const npm = node?.componentMeta?.npm;
const selected = const selected =
[npm?.package, npm?.componentName].filter((item) => !!item).join('-') || [npm?.package, npm?.componentName].filter((item) => !!item).join('-') ||

View File

@ -1218,10 +1218,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
} }
const dropContainer = this.getDropContainer(e); const dropContainer = this.getDropContainer(e);
const canDropIn = dropContainer?.container?.componentMeta?.prototype?.options?.canDropIn; const canDropIn = dropContainer?.container?.componentMeta?.prototype?.options?.canDropIn;
const lockedNode = getClosestNode(dropContainer?.container as Node, (node) => { const lockedNode = getClosestNode(dropContainer?.container as Node, (node) => node.isLocked);
return node?.getExtraProp('isLocked')?.getValue() === true;
});
// const isLocked = dropContainer?.container?.getExtraProp('isLocked')?.getValue();
if (lockedNode) return null; if (lockedNode) return null;
if ( if (
!dropContainer || !dropContainer ||

View File

@ -11,12 +11,13 @@ export const getClosestClickableNode = (
event: MouseEvent, event: MouseEvent,
) => { ) => {
let node = currentNode; let node = currentNode;
// 执行 onClickHook 来判断当前节点是否可点击
while (node) { while (node) {
const lockedNode = getClosestNode(node, (n) => { // 判断当前节点是否可点击
return n?.getExtraProp('isLocked')?.getValue() === true;
});
let canClick = canClickNode(node, event); let canClick = canClickNode(node, event);
const lockedNode = getClosestNode(node!, (n) => {
// 假如当前节点就是 locked 状态,要从当前节点的父节点开始查找
return !!(node?.isLocked ? n.parent?.isLocked : n.isLocked);
});
if (lockedNode && lockedNode.getId() !== node.getId()) { if (lockedNode && lockedNode.getId() !== node.getId()) {
canClick = false; canClick = false;
} }

View File

@ -474,12 +474,11 @@ const builtinComponentActions: ComponentAction[] = [
icon: IconUnlock, // 解锁icon icon: IconUnlock, // 解锁icon
title: intlNode('lock'), title: intlNode('lock'),
action(node: Node) { action(node: Node) {
node.getExtraProp('isLocked', true)?.setValue(true); node.lock();
}, },
}, },
condition: (node: Node) => { condition: (node: Node) => {
const isLocked = node.getExtraProp('isLocked')?.getValue(); return (engineConfig.get('enableCanvasLock', false) && node.isContainer() && !node.isLocked);
return (engineConfig.get('enableCanvasLock', false) && node.isContainer() && isLocked !== true);
}, },
important: true, important: true,
}, },
@ -489,12 +488,11 @@ const builtinComponentActions: ComponentAction[] = [
icon: IconLock, // 锁定icon icon: IconLock, // 锁定icon
title: intlNode('unlock'), title: intlNode('unlock'),
action(node: Node) { action(node: Node) {
node.getExtraProp('isLocked', true)?.setValue(false); node.lock(false);
}, },
}, },
condition: (node: Node) => { condition: (node: Node) => {
const isLocked = node.getExtraProp('isLocked')?.getValue(); return (engineConfig.get('enableCanvasLock', false) && node.isContainer() && node.isLocked);
return (engineConfig.get('enableCanvasLock', false) && node.isContainer() && isLocked === true);
}, },
important: true, important: true,
}, },

View File

@ -561,8 +561,8 @@ export class Node<Schema extends NodeSchema = NodeSchema> {
return this.props.get(getConvertedExtraKey(key), stash) || null; return this.props.get(getConvertedExtraKey(key), stash) || null;
} }
setExtraProp(key: string, value: CompositeValue, spread = false, options: any = {}): Prop | null { setExtraProp(key: string, value: CompositeValue) {
return this.props.add(value, getConvertedExtraKey(key), spread, options) || null; this.getProp(getConvertedExtraKey(key), true)?.setValue(value);
} }
/** /**

View File

@ -87,15 +87,11 @@ export default class TreeNode {
} }
@computed get locked(): boolean { @computed get locked(): boolean {
return this.node.getExtraProp('isLocked', false)?.getValue() === true; return this.node.isLocked;
} }
setLocked(flag: boolean) { setLocked(flag: boolean) {
if (flag) { this.node.lock(flag);
this.node.getExtraProp('isLocked', true)?.setValue(true);
} else {
this.node.getExtraProp('isLocked', false)?.remove();
}
} }
@computed get selected(): boolean { @computed get selected(): boolean {