import{aw as s,y as a,z as e,b0 as t}from"./chunks/framework.BCBheFgR.js";const k=JSON.parse('{"title":"editorService方法","description":"","frontmatter":{},"headers":[],"relativePath":"api/editor/editorServiceMethods.md","filePath":"api/editor/editorServiceMethods.md"}'),l={name:"api/editor/editorServiceMethods.md"};function n(r,i,p,h,o,d){return e(),a("div",null,i[0]||(i[0]=[t(`

editorService方法

get

js
import { editorService } from '@tmagic/editor';

const node = editorService.get('node');

set

js
import { editorService } from '@tmagic/editor';

const node = editorService.get('node');

editorService.set('node', {
  ...node,
  name: 'new name'
});

getNodeInfo

TIP

如果raw为false,对获取到的对象进行操作会触发vue响应式处理

js
import { editorService } from '@tmagic/editor';

const info = editorService.getNodeInfo('text_123');

console.log(info.node);
console.log(info.parent);
console.log(info.page);

getNodeById

js
import { editorService } from '@tmagic/editor';

const node = editorService.getNodeById('text_123');

console.log(node);

getParentById

js
import { editorService } from '@tmagic/editor';

const parent = editorService.getParentById('text_123');

console.log(parent);

getLayout

js
import { editorService } from '@tmagic/editor';

const parent = editorService.getParentById('text_123');
editorService.getLayout(parent).then((layout) => {
  console.log(parent);
});

select

js
import { editorService } from '@tmagic/editor';

editorService.select('text_123');
editorService.get('stage')?.select('text_123');

multiSelect

js
import { editorService } from '@tmagic/editor';

editorService.multiSelect(['text_123', 'button_123']);
editorService.get('stage')?.multiSelect(['text_123', 'button_123']);

highlight

js
import { editorService } from '@tmagic/editor';

editorService.highlight('text_123');

doAdd

add

doRemove

remove

doUpdate

update

sort

copy

复制组件节点或节点集合

通过storageService.setItem,将组将节点配置转化成string,然后存储到localStorage中

doPaste

粘贴前置操作:返回分配了新id以及校准了坐标的配置

paste

粘贴组件节点或节点集合

通过storageService.getItem,从localStorage中获取节点,然后添加到当前容器中

doAlignCenter

alignCenter

水平居中组件或者组件集合,仅在流式布局下有效

TIP

doAlignCenter的区别:

alignCenter可以支持一次水平居中多个组件,alignCenter是通过调用doAlignCenter来获取到已设置好水平居中的位置信息的节点,然后调用update更新。

moveLayer

moveToContainer

undo

redo

move

resetModifiedNodeId

重置当前记录的修改过的节点id记录,通常用于保存之后

resetState

清空state

destroy

use

使用中间件的方式扩展方法,上述方法中标记有扩展支持: 是的方法都支持使用use扩展

js
import { editorService, getAddParent } from '@tmagic/editor';
import { ElMessageBox } from 'element-plus';

editorService.use({
  // 添加是否删除节点确认提示
  async remove(node, next) {
    await ElMessageBox.confirm('是否删除', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    });

    next();
  },

  add(node, next) {
    // text组件只能添加到container中
    const parentNode = getAddParent(node);
    if (node.type === 'text' && parentNode?.type !== 'container') {
      return;
    }

    next();
  },
});

usePlugin

相对于use, usePlugin支持更加灵活更加细致的扩展, 上述方法中标记有扩展支持: 是的方法都支持使用usePlugin扩展

每个支持扩展的方法都支持定制before、after两个hook来干预原有方法的行为,before可以用于修改传入参数,after可以用于修改返回的值

js
import { editorService } from '@tmagic/editor';

editorService.usePlugin({
  // 添加组件的时候设置一个添加时间
  beforeDoAdd: (config, parent) => {
    config.addTime = new Date().getTime();

    return [config, parent];
  },
});

removeAllPlugins

删掉当前设置的所有扩展

`,92)]))}const g=s(l,[["render",n]]);export{k as __pageData,g as default};