feat: support global inline editing

This commit is contained in:
kangwei 2020-05-12 19:15:29 +08:00
parent 54242ca62a
commit 4f7179bd26
4 changed files with 78 additions and 24 deletions

View File

@ -44,7 +44,7 @@ export class LiveEditing {
let setterPropElement = getSetterPropElement(targetElement, rootElement);
let propTarget = setterPropElement?.dataset.setterProp;
let matched: LiveTextEditingConfig & { propElement?: HTMLElement; } | undefined;
let matched: (LiveTextEditingConfig & { propElement?: HTMLElement; }) | undefined | null;
if (liveTextEditing) {
if (propTarget) {
// 已埋点命中 data-setter-prop="proptarget", 从 liveTextEditing 读取配置mode|onSaveContent
@ -151,9 +151,9 @@ export class LiveEditing {
}
}
export type SpecificRule = (target: EditingTarget) => LiveTextEditingConfig & {
export type SpecificRule = (target: EditingTarget) => (LiveTextEditingConfig & {
propElement?: HTMLElement;
};
}) | null;
export interface SaveHandler {
condition: (prop: Prop) => boolean;

View File

@ -175,7 +175,7 @@ export class ComponentMeta {
});
}
collectLiveTextEditing(this.configure);
this._liveTextEditing = liveTextEditing;
this._liveTextEditing = liveTextEditing.length > 0 ? liveTextEditing : undefined;
const { configure = {} } = this._transformedMetadata;
this._acceptable = false;

View File

@ -10,6 +10,7 @@ import { Skeleton, SettingsPrimaryPane } from '@ali/lowcode-editor-skeleton';
import { i18nReducer } from './i18n-reducer';
import { InstanceNodeSelector } from './components';
import { liveEditingRule } from './vc-live-editing';
export const editor = new Editor();
globalContext.register(editor, Editor);
@ -160,26 +161,7 @@ skeleton.add({
content: OutlineBackupPane,
});
LiveEditing.addLiveEditingSpecificRule((target) => {
// TODO: enhance for legao specific
const contentValue = target.node.getPropValue('content');
return null;
});
// skeleton.add({
// name: 'sourceEditor',
// type: 'PanelDock',
// props: {
// align: 'top',
// icon: 'code',
// description: '组件库',
// },
// panelProps: {
// width: 500
// // area: 'leftFixedArea'
// },
// content: SourceEditor,
// });
LiveEditing.addLiveEditingSpecificRule(liveEditingRule);
// 实例节点选择器,线框高亮
addBuiltinComponentAction({

View File

@ -0,0 +1,72 @@
import { EditingTarget, Node as DocNode } from '@ali/lowcode-designer';
import Env from './env';
import { isJSExpression } from '@ali/lowcode-types';
const I18nUtil = require('@ali/ve-i18n-util');
interface I18nObject {
type?: string;
use?: string;
key?: string;
[lang: string]: string | undefined;
}
function getI18nText(obj: I18nObject) {
let locale = Env.getLocale();
if (obj.key) {
return I18nUtil.get(obj.key, locale);
}
if (locale !== 'zh_CN' && locale !== 'zh_TW' && !obj[locale]) {
locale = 'en_US';
}
return obj[obj.use || locale] || obj.zh_CN;
}
function getText(node: DocNode, prop: string) {
const p = node.getProp(prop, false);
if (!p || p.isUnset()) {
return null;
}
const v = p.getValue();
if (v == null) {
return null;
}
if (p.type === 'literal') {
return v;
}
if ((v as any).type === 'i18n') {
return getI18nText(v as any);
}
if (isJSExpression(v)) {
return v.mock;
}
}
export function liveEditingRule(target: EditingTarget) {
// for vision components specific
const { node, rootElement, event } = target;
const targetElement = event.target as HTMLElement;
if (!Array.from(targetElement.childNodes).every(item => item.nodeType === Node.TEXT_NODE)) {
return null;
}
const innerText = targetElement.innerText;
const propTarget = ['title', 'label', 'text', 'content'].find(prop => {
// TODO: enhance compare text logic
return getText(node, prop) === innerText;
});
if (propTarget) {
return {
propElement: targetElement,
propTarget,
};
}
return null;
}
// TODO:
export function liveEditingSaveHander() {
}