feat: get editor from this or params

This commit is contained in:
liujuping 2023-04-26 15:34:02 +08:00 committed by 林熠
parent 9fd28efd0e
commit 70120a033a
11 changed files with 53 additions and 62 deletions

View File

@ -1,10 +1,10 @@
import React, { Component, Fragment } from 'react';
import DragResizeEngine from './drag-resize-engine';
import { observer, computed, globalContext } from '@alilc/lowcode-editor-core';
import { observer, computed } from '@alilc/lowcode-editor-core';
import classNames from 'classnames';
import { SimulatorContext } from '../context';
import { BuiltinSimulatorHost } from '../host';
import { OffsetObserver, Designer } from '../../designer';
import { OffsetObserver, Designer, INode } from '../../designer';
import { Node } from '../../document';
import { normalizeTriggers } from '../../utils/misc';
@ -135,7 +135,7 @@ export class BoxResizingInstance extends Component<{
// this.hoveringCapture.setBoundary(this.outline);
this.willBind();
const resize = (e: MouseEvent, direction: string, node: any, moveX: number, moveY: number) => {
const resize = (e: MouseEvent, direction: string, node: INode, moveX: number, moveY: number) => {
const { advanced } = node.componentMeta;
if (
advanced.callbacks &&
@ -149,7 +149,7 @@ export class BoxResizingInstance extends Component<{
}
};
const resizeStart = (e: MouseEvent, direction: string, node: any) => {
const resizeStart = (e: MouseEvent, direction: string, node: INode) => {
const { advanced } = node.componentMeta;
if (
advanced.callbacks &&
@ -161,7 +161,7 @@ export class BoxResizingInstance extends Component<{
}
};
const resizeEnd = (e: MouseEvent, direction: string, node: any) => {
const resizeEnd = (e: MouseEvent, direction: string, node: INode) => {
const { advanced } = node.componentMeta;
if (
advanced.callbacks &&
@ -172,8 +172,7 @@ export class BoxResizingInstance extends Component<{
advanced.callbacks.onResizeEnd(e, cbNode);
}
const workspace = globalContext.get('workspace');
const editor = workspace.isActive ? workspace.window.editor : globalContext.get('editor');
const editor = node.document?.designer.editor;
const npm = node?.componentMeta?.npm;
const selected =
[npm?.package, npm?.componentName].filter((item) => !!item).join('-') ||

View File

@ -9,7 +9,7 @@ import {
ComponentType,
} from 'react';
import classNames from 'classnames';
import { observer, computed, Tip, globalContext } from '@alilc/lowcode-editor-core';
import { observer, computed, Tip } from '@alilc/lowcode-editor-core';
import { createIcon, isReactComponent, isActionContentObject } from '@alilc/lowcode-utils';
import { IPublicTypeActionContentObject } from '@alilc/lowcode-types';
import { BuiltinSimulatorHost } from '../host';
@ -131,8 +131,7 @@ function createAction(content: ReactNode | ComponentType<any> | IPublicTypeActio
className="lc-borders-action"
onClick={() => {
action && action(node.internalToShellNode()!);
const workspace = globalContext.get('workspace');
const editor = workspace.isActive ? workspace.window.editor : globalContext.get('editor');
const editor = node.document?.designer.editor;
const npm = node?.componentMeta?.npm;
const selected =
[npm?.package, npm?.componentName].filter((item) => !!item).join('-') ||

View File

@ -1,6 +1,6 @@
import { obx, globalContext } from '@alilc/lowcode-editor-core';
import { obx } from '@alilc/lowcode-editor-core';
import { IPublicTypePluginConfig, IPublicTypeLiveTextEditingConfig } from '@alilc/lowcode-types';
import { Node, Prop } from '../../document';
import { INode, Prop } from '../../document';
const EDITOR_KEY = 'data-setter-prop';
@ -17,7 +17,7 @@ function defaultSaveContent(content: string, prop: Prop) {
}
export interface EditingTarget {
node: Node;
node: INode;
rootElement: HTMLElement;
event: MouseEvent;
}
@ -47,13 +47,16 @@ export class LiveEditing {
@obx.ref private _editing: Prop | null = null;
private _dispose?: () => void;
private _save?: () => void;
apply(target: EditingTarget) {
const { node, event, rootElement } = target;
const targetElement = event.target as HTMLElement;
const { liveTextEditing } = node.componentMeta;
const workspace = globalContext.get('workspace');
const editor = workspace.isActive ? workspace.window.editor : globalContext.get('editor');
const editor = node.document?.designer.editor;
const npm = node?.componentMeta?.npm;
const selected =
[npm?.package, npm?.componentName].filter((item) => !!item).join('-') || node?.componentMeta?.componentName || '';
@ -166,10 +169,6 @@ export class LiveEditing {
return this._editing;
}
private _dispose?: () => void;
private _save?: () => void;
saveAndDispose() {
if (this._save) {
this._save();

View File

@ -1,6 +1,6 @@
import { Overlay } from '@alifd/next';
import React, { MouseEvent } from 'react';
import { Title, globalContext } from '@alilc/lowcode-editor-core';
import { Title } from '@alilc/lowcode-editor-core';
import { canClickNode } from '@alilc/lowcode-utils';
import './index.less';
@ -66,8 +66,7 @@ export default class InstanceNodeSelector extends React.Component<IProps, IState
if (canClick && typeof node.select === 'function') {
node.select();
const workspace = globalContext.get('workspace');
const editor = workspace.isActive ? workspace.window.editor : globalContext.get('editor');
const editor = node.document?.designer.editor;
const npm = node?.componentMeta?.npm;
const selected =
[npm?.package, npm?.componentName].filter((item) => !!item).join('-') ||

View File

@ -60,6 +60,8 @@ export function buildFilter(rule?: string | string[] | RegExp | IPublicTypeNesti
export interface IComponentMeta extends IPublicModelComponentMeta<INode> {
prototype?: any;
liveTextEditing?: IPublicTypeLiveTextEditingConfig[];
get rootSelector(): string | undefined;
setMetadata(metadata: IPublicTypeComponentMetadata): void;

View File

@ -148,6 +148,10 @@ export interface IDocumentModel extends Omit<IPublicModelDocumentModel<
suspense(): void;
close(): void;
unlinkNode(node: INode): void;
destroyNode(node: INode): void;
}
export class DocumentModel implements IDocumentModel {
@ -333,6 +337,7 @@ export class DocumentModel implements IDocumentModel {
this.import(schema as IPublicTypeRootSchema, true);
this.simulator?.rerender();
},
this,
);
this.setupListenActiveNodes();

View File

@ -1,6 +1,7 @@
import { reaction, untracked, globalContext, IEventBus, createModuleEventBus } from '@alilc/lowcode-editor-core';
import { reaction, untracked, IEventBus, createModuleEventBus } from '@alilc/lowcode-editor-core';
import { IPublicTypeNodeSchema, IPublicModelHistory, IPublicTypeDisposable } from '@alilc/lowcode-types';
import { Logger } from '@alilc/lowcode-utils';
import { IDocumentModel } from '../designer';
const logger = new Logger({ level: 'warn', bizName: 'history' });
@ -37,10 +38,12 @@ export class History<T = IPublicTypeNodeSchema> implements IHistory {
return this.session.data;
}
private timeGap: number = 1000;
constructor(
dataFn: () => T | null,
private redoer: (data: T) => void,
private timeGap: number = 1000,
private document?: IDocumentModel,
) {
this.session = new Session(0, null, this.timeGap);
this.records = [this.session];
@ -130,8 +133,7 @@ export class History<T = IPublicTypeNodeSchema> implements IHistory {
}
const cursor = this.session.cursor - 1;
this.go(cursor);
const workspace = globalContext.get('workspace');
const editor = workspace.isActive ? workspace.window.editor : globalContext.get('editor');
const editor = this.document?.designer.editor;
if (!editor) {
return;
}
@ -144,8 +146,7 @@ export class History<T = IPublicTypeNodeSchema> implements IHistory {
}
const cursor = this.session.cursor + 1;
this.go(cursor);
const workspace = globalContext.get('workspace');
const editor = workspace.isActive ? workspace.window.editor : globalContext.get('editor');
const editor = this.document?.designer.editor;
if (!editor) {
return;
}

View File

@ -1,4 +1,4 @@
import { obx, computed, globalContext, makeObservable, IEventBus, createModuleEventBus } from '@alilc/lowcode-editor-core';
import { obx, computed, makeObservable, IEventBus, createModuleEventBus } from '@alilc/lowcode-editor-core';
import { Node, INode } from './node';
import { IPublicTypeNodeData, IPublicModelNodeChildren, IPublicEnumTransformStage, IPublicTypeDisposable } from '@alilc/lowcode-types';
import { shallowEqual, compatStage, isNodeSchema } from '@alilc/lowcode-utils';
@ -16,12 +16,12 @@ export interface INodeChildren extends Omit<IPublicModelNodeChildren<INode>,
'isEmpty' |
'notEmpty'
> {
children: INode[];
get owner(): INode;
get length(): number;
children: INode[];
unlinkChild(node: INode): void;
/**
@ -239,11 +239,8 @@ export class NodeChildren implements INodeChildren {
}
const { document } = node;
/* istanbul ignore next */
if (globalContext.has('editor')) {
const workspace = globalContext.get('workspace');
const editor = workspace.isActive ? workspace.window.editor : globalContext.get('editor');
editor.eventBus.emit('node.remove', { node, index: i });
}
const editor = node.document?.designer.editor;
editor?.eventBus.emit('node.remove', { node, index: i });
document?.unlinkNode(node);
document?.selection.remove(node.id);
document?.destroyNode(node);
@ -281,14 +278,11 @@ export class NodeChildren implements INodeChildren {
const i = children.map(d => d.id).indexOf(node.id);
if (node.parent) {
if (globalContext.has('editor')) {
const workspace = globalContext.get('workspace');
const editor = workspace.isActive ? workspace.window.editor : globalContext.get('editor');
editor.eventBus.emit('node.remove.topLevel', {
node,
index: node.index,
});
}
const editor = node.document?.designer.editor;
editor?.eventBus.emit('node.remove.topLevel', {
node,
index: node.index,
});
}
if (i < 0) {
@ -317,11 +311,8 @@ export class NodeChildren implements INodeChildren {
});
this.emitter.emit('insert', node);
/* istanbul ignore next */
if (globalContext.has('editor')) {
const workspace = globalContext.get('workspace');
const editor = workspace.isActive ? workspace.window.editor : globalContext.get('editor');
editor.eventBus.emit('node.add', { node });
}
const editor = node.document?.designer.editor;
editor?.eventBus.emit('node.add', { node });
if (useMutator) {
this.reportModified(node, this.owner, { type: 'insert' });
}

View File

@ -53,8 +53,7 @@ export class SettingsPrimaryPane extends Component<ISettingsPrimaryPaneProps, {
}
renderBreadcrumb() {
const { settings } = this.main;
const { config } = this.props;
const { settings, editor } = this.main;
// const shouldIgnoreRoot = config.props?.ignoreRoot;
const { shouldIgnoreRoot } = this.state;
if (!settings) {
@ -73,8 +72,6 @@ export class SettingsPrimaryPane extends Component<ISettingsPrimaryPaneProps, {
);
}
const workspace = globalContext.get('workspace');
const editor = this.props.engineEditor;
const designer = editor.get('designer');
const current = designer?.currentSelection?.getNodes()?.[0];
let node: INode | null = settings.first;

View File

@ -1,7 +1,7 @@
import { Component, ReactElement } from 'react';
import { Icon } from '@alifd/next';
import classNames from 'classnames';
import { Title, observer, Tip, globalContext } from '@alilc/lowcode-editor-core';
import { Title, observer, Tip } from '@alilc/lowcode-editor-core';
import { DockProps } from '../../types';
import { PanelDock } from '../../widget/panel-dock';
import { composeTitle } from '../../widget/utils';
@ -116,14 +116,12 @@ export class DraggableLineView extends Component<{ panel: Panel }> {
}
// 抛出事件,对于有些需要 panel 插件随着 度变化进行再次渲染的由panel插件内部监听事件实现
const workspace = globalContext.get('workspace');
const editor = workspace.isActive ? workspace.window.editor : globalContext.get('editor');
const editor = this.props.panel.skeleton.editor;
editor?.eventBus.emit('dockpane.drag', width);
}
onDragChange(type: 'start' | 'end') {
const workspace = globalContext.get('workspace');
const editor = workspace.isActive ? workspace.window.editor : globalContext.get('editor');
const editor = this.props.panel.skeleton.editor;
editor?.eventBus.emit('dockpane.dragchange', type);
// builtinSimulator 屏蔽掉 鼠标事件
editor?.eventBus.emit('designer.builtinSimulator.disabledEvents', type === 'start');
@ -187,8 +185,7 @@ export class TitledPanelView extends Component<{ panel: Panel; area?: string }>
if (!panel.inited) {
return null;
}
const workspace = globalContext.get('workspace');
const editor = workspace.isActive ? workspace.window.editor : globalContext.get('editor');
const editor = panel.skeleton.editor;
const panelName = area ? `${area}-${panel.name}` : panel.name;
editor?.eventBus.emit('skeleton.panel.toggle', {
name: panelName || '',
@ -250,8 +247,7 @@ export class PanelView extends Component<{
if (!panel.inited) {
return null;
}
const workspace = globalContext.get('workspace');
const editor = workspace.isActive ? workspace.window.editor : globalContext.get('editor');
const editor = panel.skeleton.editor;
const panelName = area ? `${area}-${panel.name}` : panel.name;
editor?.eventBus.emit('skeleton.panel.toggle', {
name: panelName || '',

View File

@ -183,6 +183,9 @@ export class Workspace implements IWorkspace {
}
private remove(index: number) {
if (index < 0) {
return;
}
const window = this.windows[index];
this.windows.splice(index, 1);
if (this.window === window) {