mirror of
				https://github.com/Tencent/tmagic-editor.git
				synced 2025-11-04 10:49:51 +08:00 
			
		
		
		
	fix(editor): 删除全部页面后显示异常
This commit is contained in:
		
							parent
							
								
									d1610e5ff2
								
							
						
					
					
						commit
						a0c3e25edb
					
				@ -49,20 +49,20 @@ export default defineComponent({
 | 
				
			|||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  setup() {
 | 
					  setup() {
 | 
				
			||||||
    const services = inject<Services>('services');
 | 
					    const { editorService, uiService } = inject<Services>('services') || {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const root = computed(() => services?.editorService.get<MApp>('root'));
 | 
					    const root = computed(() => editorService?.get<MApp>('root'));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
      root,
 | 
					      root,
 | 
				
			||||||
      pageLength: computed(() => root.value?.items?.length || 0),
 | 
					      pageLength: computed(() => editorService?.get<number>('pageLength') || 0),
 | 
				
			||||||
      showSrc: computed(() => services?.uiService.get<boolean>('showSrc')),
 | 
					      showSrc: computed(() => uiService?.get<boolean>('showSrc')),
 | 
				
			||||||
      columnWidth: computed(() => services?.uiService.get<GetColumnWidth>('columnWidth')),
 | 
					      columnWidth: computed(() => uiService?.get<GetColumnWidth>('columnWidth')),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      saveCode(value: string) {
 | 
					      saveCode(value: string) {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
          // eslint-disable-next-line no-eval
 | 
					          // eslint-disable-next-line no-eval
 | 
				
			||||||
          services?.editorService.set('root', eval(value));
 | 
					          editorService?.set('root', eval(value));
 | 
				
			||||||
        } catch (e: any) {
 | 
					        } catch (e: any) {
 | 
				
			||||||
          console.error(e);
 | 
					          console.error(e);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
@ -54,6 +54,7 @@ class Editor extends BaseService {
 | 
				
			|||||||
    stage: null,
 | 
					    stage: null,
 | 
				
			||||||
    highlightNode: null,
 | 
					    highlightNode: null,
 | 
				
			||||||
    modifiedNodeIds: new Map(),
 | 
					    modifiedNodeIds: new Map(),
 | 
				
			||||||
 | 
					    pageLength: 0,
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor() {
 | 
					  constructor() {
 | 
				
			||||||
@ -90,6 +91,7 @@ class Editor extends BaseService {
 | 
				
			|||||||
    log('store set ', name, ' ', value);
 | 
					    log('store set ', name, ' ', value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (name === 'root') {
 | 
					    if (name === 'root') {
 | 
				
			||||||
 | 
					      this.state.pageLength = (value as unknown as MApp).items.length;
 | 
				
			||||||
      this.emit('root-change', value);
 | 
					      this.emit('root-change', value);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@ -253,8 +255,9 @@ class Editor extends BaseService {
 | 
				
			|||||||
    const curNode = this.get<MContainer>('node');
 | 
					    const curNode = this.get<MContainer>('node');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let parentNode: MNode | undefined;
 | 
					    let parentNode: MNode | undefined;
 | 
				
			||||||
 | 
					    const isPage = type === NodeType.PAGE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (type === NodeType.PAGE) {
 | 
					    if (isPage) {
 | 
				
			||||||
      parentNode = this.get<MApp>('root');
 | 
					      parentNode = this.get<MApp>('root');
 | 
				
			||||||
      // 由于支持中间件扩展,在parent参数为undefined时,parent会变成next函数
 | 
					      // 由于支持中间件扩展,在parent参数为undefined时,parent会变成next函数
 | 
				
			||||||
    } else if (parent && typeof parent !== 'function') {
 | 
					    } else if (parent && typeof parent !== 'function') {
 | 
				
			||||||
@ -294,6 +297,10 @@ class Editor extends BaseService {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    stage?.select(newNode.id);
 | 
					    stage?.select(newNode.id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (isPage) {
 | 
				
			||||||
 | 
					      this.state.pageLength += 1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this.emit('add', newNode);
 | 
					    this.emit('add', newNode);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return newNode;
 | 
					    return newNode;
 | 
				
			||||||
@ -324,9 +331,23 @@ class Editor extends BaseService {
 | 
				
			|||||||
    stage?.remove({ id: node.id, root: this.get('root') });
 | 
					    stage?.remove({ id: node.id, root: this.get('root') });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (node.type === NodeType.PAGE) {
 | 
					    if (node.type === NodeType.PAGE) {
 | 
				
			||||||
 | 
					      this.state.pageLength -= 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (root.items[0]) {
 | 
					      if (root.items[0]) {
 | 
				
			||||||
        await this.select(root.items[0]);
 | 
					        await this.select(root.items[0]);
 | 
				
			||||||
        stage?.select(root.items[0].id);
 | 
					        stage?.select(root.items[0].id);
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        this.set('node', null);
 | 
				
			||||||
 | 
					        this.set('parent', null);
 | 
				
			||||||
 | 
					        this.set('page', null);
 | 
				
			||||||
 | 
					        this.set('stage', null);
 | 
				
			||||||
 | 
					        this.set('highlightNode', null);
 | 
				
			||||||
 | 
					        this.resetModifiedNodeId();
 | 
				
			||||||
 | 
					        historyService.reset();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        this.emit('remove', node);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return node;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      await this.select(parent);
 | 
					      await this.select(parent);
 | 
				
			||||||
 | 
				
			|||||||
@ -51,6 +51,13 @@ class History extends BaseService {
 | 
				
			|||||||
    this.on('change', this.setCanUndoRedo);
 | 
					    this.on('change', this.setCanUndoRedo);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public reset() {
 | 
				
			||||||
 | 
					    this.state.pageSteps = {};
 | 
				
			||||||
 | 
					    this.state.pageId = undefined;
 | 
				
			||||||
 | 
					    this.state.canRedo = false;
 | 
				
			||||||
 | 
					    this.state.canUndo = false;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public changePage(page: MPage): void {
 | 
					  public changePage(page: MPage): void {
 | 
				
			||||||
    if (!page) return;
 | 
					    if (!page) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -53,6 +53,7 @@ export interface StoreState {
 | 
				
			|||||||
  highlightNode: MNode | null;
 | 
					  highlightNode: MNode | null;
 | 
				
			||||||
  stage: StageCore | null;
 | 
					  stage: StageCore | null;
 | 
				
			||||||
  modifiedNodeIds: Map<Id, Id>;
 | 
					  modifiedNodeIds: Map<Id, Id>;
 | 
				
			||||||
 | 
					  pageLength: number;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export interface PropsState {
 | 
					export interface PropsState {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user