Skip to content

Commit

Permalink
feat: get editor from this or params
Browse files Browse the repository at this point in the history
  • Loading branch information
liujuping authored and JackLian committed Apr 26, 2023
1 parent 9fd28ef commit 70120a0
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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 &&
Expand All @@ -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('-') ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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('-') ||
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -17,7 +17,7 @@ function defaultSaveContent(content: string, prop: Prop) {
}

export interface EditingTarget {
node: Node;
node: INode;
rootElement: HTMLElement;
event: MouseEvent;
}
Expand Down Expand Up @@ -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 || '';
Expand Down Expand Up @@ -166,10 +169,6 @@ export class LiveEditing {
return this._editing;
}

private _dispose?: () => void;

private _save?: () => void;

saveAndDispose() {
if (this._save) {
this._save();
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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('-') ||
Expand Down
2 changes: 2 additions & 0 deletions packages/designer/src/component-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions packages/designer/src/document/document-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -333,6 +337,7 @@ export class DocumentModel implements IDocumentModel {
this.import(schema as IPublicTypeRootSchema, true);
this.simulator?.rerender();
},
this,
);

this.setupListenActiveNodes();
Expand Down
13 changes: 7 additions & 6 deletions packages/designer/src/document/history.ts
Original file line number Diff line number Diff line change
@@ -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' });

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
33 changes: 12 additions & 21 deletions packages/designer/src/document/node/node-children.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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' });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
14 changes: 5 additions & 9 deletions packages/editor-skeleton/src/components/widget-views/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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 || '',
Expand Down Expand Up @@ -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 || '',
Expand Down
3 changes: 3 additions & 0 deletions packages/workspace/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 70120a0

Please sign in to comment.