Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix workspace api ts defined #1753

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ module.exports = {
'error',
{ default: ['signature', 'field', 'constructor', 'method'] }
],
'no-unused-vars': ['error', { "destructuredArrayIgnorePattern": "^_" }]
'@typescript-eslint/no-unused-vars': ['error']
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { observer, computed, Tip, globalContext } from '@alilc/lowcode-editor-co
import { createIcon, isReactComponent, isActionContentObject } from '@alilc/lowcode-utils';
import { IPublicTypeActionContentObject } from '@alilc/lowcode-types';
import { BuiltinSimulatorHost } from '../host';
import { OffsetObserver } from '../../designer';
import { Node } from '../../document';
import { INode, OffsetObserver } from '../../designer';
import NodeSelector from '../node-selector';
import { ISimulatorHost } from '../../simulator';

@observer
export class BorderSelectingInstance extends Component<{
Expand Down Expand Up @@ -116,8 +116,8 @@ class Toolbar extends Component<{ observed: OffsetObserver }> {
}
}

function createAction(content: ReactNode | ComponentType<any> | IPublicTypeActionContentObject, key: string, node: Node) {
if (isValidElement(content)) {
function createAction(content: ReactNode | ComponentType<any> | IPublicTypeActionContentObject, key: string, node: INode) {
if (isValidElement<{ key: string; node: INode }>(content)) {
return cloneElement(content, { key, node });
}
if (isReactComponent(content)) {
Expand All @@ -130,7 +130,7 @@ function createAction(content: ReactNode | ComponentType<any> | IPublicTypeActio
key={key}
className="lc-borders-action"
onClick={() => {
action && action(node);
action && action(node.internalToShellNode()!);
const workspace = globalContext.get('workspace');
const editor = workspace.isActive ? workspace.window.editor : globalContext.get('editor');
const npm = node?.componentMeta?.npm;
Expand All @@ -153,8 +153,8 @@ function createAction(content: ReactNode | ComponentType<any> | IPublicTypeActio
}

@observer
export class BorderSelectingForNode extends Component<{ host: BuiltinSimulatorHost; node: Node }> {
get host(): BuiltinSimulatorHost {
export class BorderSelectingForNode extends Component<{ host: ISimulatorHost; node: INode }> {
get host(): ISimulatorHost {
return this.props.host;
}

Expand Down
65 changes: 34 additions & 31 deletions packages/designer/src/builtin-simulator/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,26 @@ import {
getRectTarget,
CanvasPoint,
Designer,
IDesigner,
} from '../designer';
import { parseMetadata } from './utils/parse-metadata';
import { getClosestClickableNode } from './utils/clickable';
import {
IPublicTypeComponentMetadata,
IPublicTypeComponentSchema,
IPublicTypePackage,
IPublicEnumTransitionType,
IPublicEnumDragObjectType,
IPublicTypeDragNodeObject,
IPublicTypeNodeInstance,
IPublicTypeComponentInstance,
IPublicTypeLocationChildrenDetail,
IPublicTypeLocationDetailType,
IPublicTypeRect,
IPublicModelNode,
} from '@alilc/lowcode-types';
import { BuiltinSimulatorRenderer } from './renderer';
import { clipboard } from '../designer/clipboard';
import { LiveEditing } from './live-editing/live-editing';
import { Project } from '../project';
import { IProject, Project } from '../project';
import { IScroller } from '../designer/scroller';
import { isElementNode, isDOMNodeVisible } from '../utils/misc';
import { debounce } from 'lodash';
Expand Down Expand Up @@ -164,9 +164,9 @@ const defaultRaxEnvironment = [
export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProps> {
readonly isSimulator = true;

readonly project: Project;
readonly project: IProject;

readonly designer: Designer;
readonly designer: IDesigner;

readonly viewport = new Viewport();

Expand Down Expand Up @@ -217,7 +217,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
return this.get('requestHandlersMap') || null;
}

get thisRequiredInJSE(): any {
get thisRequiredInJSE(): boolean {
return engineConfig.get('thisRequiredInJSE') ?? true;
}

Expand Down Expand Up @@ -559,8 +559,8 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
return;
}
// FIXME: dirty fix remove label-for fro liveEditing
(downEvent.target as HTMLElement).removeAttribute('for');
const nodeInst = this.getNodeInstanceFromElement(downEvent.target as Element);
downEvent.target?.removeAttribute('for');
const nodeInst = this.getNodeInstanceFromElement(downEvent.target);
const { focusNode } = documentModel;
const node = getClosestClickableNode(nodeInst?.node || focusNode, downEvent);
// 如果找不到可点击的节点,直接返回
Expand Down Expand Up @@ -675,11 +675,11 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
const x = new Event('click');
x.initEvent('click', true);
this._iframe?.dispatchEvent(x);
const target = e.target as HTMLElement;
const target = e.target;

const customizeIgnoreSelectors = engineConfig.get('customizeIgnoreSelectors');
// TODO: need more elegant solution to ignore click events of components in designer
const defaultIgnoreSelectors: any = [
const defaultIgnoreSelectors: string[] = [
'.next-input-group',
'.next-checkbox-group',
'.next-checkbox-wrapper',
Expand Down Expand Up @@ -741,7 +741,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
}
};
const leave = () => {
this.project.currentDocument && detecting.leave(this.project.currentDocument)
this.project.currentDocument && detecting.leave(this.project.currentDocument);
};

doc.addEventListener('mouseover', hover, true);
Expand Down Expand Up @@ -812,7 +812,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
/**
* @see ISimulator
*/
setSuspense(suspended: boolean) {
setSuspense(/** _suspended: boolean */) {
return false;
// if (suspended) {
// /*
Expand Down Expand Up @@ -897,7 +897,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
return this.renderer?.getComponent(componentName) || null;
}

createComponent(schema: IPublicTypeComponentSchema): Component | null {
createComponent(/** _schema: IPublicTypeComponentSchema */): Component | null {
return null;
// return this.renderer?.createComponent(schema) || null;
}
Expand Down Expand Up @@ -1018,7 +1018,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
}

if (last) {
const r: any = new DOMRect(last.x, last.y, last.r - last.x, last.b - last.y);
const r: IPublicTypeRect = new DOMRect(last.x, last.y, last.r - last.x, last.b - last.y);
r.elements = elements;
r.computed = _computed;
return r;
Expand Down Expand Up @@ -1186,13 +1186,14 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
*/
locate(e: ILocateEvent): any {
const { dragObject } = e;
const { nodes } = dragObject as IPublicTypeDragNodeObject;

const nodes = dragObject?.nodes;

const operationalNodes = nodes?.filter((node) => {
const onMoveHook = node.componentMeta?.advanced.callbacks?.onMoveHook;
const canMove = onMoveHook && typeof onMoveHook === 'function' ? onMoveHook(node.internalToShellNode()) : true;

let parentContainerNode: Node | null = null;
let parentContainerNode: INode | null = null;
let parentNode = node.parent;

while (parentNode) {
Expand Down Expand Up @@ -1254,7 +1255,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
};

const locationData = {
target: container as INode,
target: container,
detail,
source: `simulator${document.id}`,
event: e,
Expand All @@ -1279,12 +1280,12 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
return this.designer.createLocation(locationData);
}

let nearRect = null;
let nearIndex = 0;
let nearNode = null;
let nearDistance = null;
let minTop = null;
let maxBottom = null;
let nearRect: IPublicTypeRect | null = null;
let nearIndex: number = 0;
let nearNode: INode | null = null;
let nearDistance: number | null = null;
let minTop: number | null = null;
let maxBottom: number | null = null;

for (let i = 0, l = children.size; i < l; i++) {
const node = children.get(i)!;
Expand Down Expand Up @@ -1341,8 +1342,13 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
const vertical = inline || row;

// TODO: fix type
const near: any = {
node: nearNode,
const near: {
node: IPublicModelNode;
pos: 'before' | 'after' | 'replace';
rect?: IPublicTypeRect;
align?: 'V' | 'H';
} = {
node: nearNode.internalToShellNode()!,
pos: 'before',
align: vertical ? 'V' : 'H',
};
Expand Down Expand Up @@ -1465,7 +1471,7 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
container = container.parent;
instance = this.getClosestNodeInstance(dropContainer.instance, container.id)?.instance;
dropContainer = {
container: container,
container,
instance,
};
} else {
Expand All @@ -1483,12 +1489,12 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
/**
* 控制接受
*/
handleAccept({ container, instance }: DropContainer, e: ILocateEvent): boolean {
handleAccept({ container }: DropContainer, e: ILocateEvent): boolean {
const { dragObject } = e;
const document = this.currentDocument!;
const focusNode = document.focusNode;
if (isRootNode(container) || container.contains(focusNode)) {
return document.checkNesting(focusNode, dragObject as any);
return document.checkNesting(focusNode!, dragObject as any);
}

const meta = (container as Node).componentMeta;
Expand All @@ -1509,15 +1515,12 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
getNearByContainer(
{ container, instance }: DropContainer,
drillDownExcludes: Set<INode>,
e: ILocateEvent,
) {
const { children } = container;
const document = this.project.currentDocument!;
if (!children || children.isEmpty()) {
return null;
}

const nearDistance: any = null;
const nearBy: any = null;
for (let i = 0, l = children.size; i < l; i++) {
let child = children.get(i);
Expand Down
30 changes: 17 additions & 13 deletions packages/designer/src/builtin-simulator/node-selector/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Overlay } from '@alifd/next';
import React from 'react';
import React, { MouseEvent } from 'react';
import { Title, globalContext } from '@alilc/lowcode-editor-core';
import { canClickNode } from '@alilc/lowcode-utils';
import './index.less';

import { Node, INode } from '@alilc/lowcode-designer';
import { INode } from '@alilc/lowcode-designer';

const { Popup } = Overlay;

export interface IProps {
node: Node;
node: INode;
}

export interface IState {
parentNodes: Node[];
parentNodes: INode[];
}

type UnionNode = INode | null;
Expand All @@ -26,14 +26,18 @@ export default class InstanceNodeSelector extends React.Component<IProps, IState
componentDidMount() {
const parentNodes = this.getParentNodes(this.props.node);
this.setState({
parentNodes,
parentNodes: parentNodes ?? [],
});
}

// 获取节点的父级节点(最多获取 5 层)
getParentNodes = (node: Node) => {
getParentNodes = (node: INode) => {
const parentNodes: any[] = [];
const { focusNode } = node.document;
const focusNode = node.document?.focusNode;

if (!focusNode) {
return null;
}

if (node.contains(focusNode) || !focusNode.contains(node)) {
return parentNodes;
Expand All @@ -53,12 +57,12 @@ export default class InstanceNodeSelector extends React.Component<IProps, IState
return parentNodes;
};

onSelect = (node: Node) => (e: unknown) => {
onSelect = (node: INode) => (event: MouseEvent) => {
if (!node) {
return;
}

const canClick = canClickNode(node, e as MouseEvent);
const canClick = canClickNode(node.internalToShellNode()!, event);

if (canClick && typeof node.select === 'function') {
node.select();
Expand All @@ -76,19 +80,19 @@ export default class InstanceNodeSelector extends React.Component<IProps, IState
}
};

onMouseOver = (node: Node) => (_: any, flag = true) => {
onMouseOver = (node: INode) => (_: any, flag = true) => {
if (node && typeof node.hover === 'function') {
node.hover(flag);
}
};

onMouseOut = (node: Node) => (_: any, flag = false) => {
onMouseOut = (node: INode) => (_: any, flag = false) => {
if (node && typeof node.hover === 'function') {
node.hover(flag);
}
};

renderNodes = (/* node: Node */) => {
renderNodes = () => {
const nodes = this.state.parentNodes;
if (!nodes || nodes.length < 1) {
return null;
Expand Down Expand Up @@ -136,7 +140,7 @@ export default class InstanceNodeSelector extends React.Component<IProps, IState
triggerType="hover"
offset={[0, 0]}
>
<div className="instance-node-selector">{this.renderNodes(node)}</div>
<div className="instance-node-selector">{this.renderNodes()}</div>
</Popup>
</div>
);
Expand Down
Loading