Skip to content

Commit

Permalink
feat(board): handle apply error
Browse files Browse the repository at this point in the history
  • Loading branch information
pubuzhixing8 committed Jun 20, 2024
1 parent 4cc414c commit be5c386
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 20 deletions.
9 changes: 9 additions & 0 deletions .changeset/cuddly-insects-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@plait/angular-board': patch
---

handle apply error

add getBoardComponentInjector to export injector

resolve type error for common/utils/text.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ChangeDetectorRef, EventEmitter, ViewContainerRef } from '@angular/core';
import { ChangeDetectorRef, EventEmitter, Injector, ViewContainerRef } from '@angular/core';
import { OnChangeData } from '../plugins/angular-board';

export interface BoardComponentInterface {
markForCheck: () => void;
cdr: ChangeDetectorRef;
nativeElement: HTMLElement;
viewContainerRef: ViewContainerRef;
injector: Injector;
onChange: EventEmitter<OnChangeData>;
}
2 changes: 2 additions & 0 deletions packages/angular-board/src/board/board.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ElementRef,
EventEmitter,
HostBinding,
Injector,
Input,
NgZone,
OnChanges,
Expand Down Expand Up @@ -180,6 +181,7 @@ export class PlaitBoardComponent implements BoardComponentInterface, OnInit, OnC

constructor(
public cdr: ChangeDetectorRef,
public injector: Injector,
public viewContainerRef: ViewContainerRef,
private elementRef: ElementRef<HTMLElement>,
private ngZone: NgZone
Expand Down
11 changes: 10 additions & 1 deletion packages/angular-board/src/plugins/angular-board.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { PlaitElement, PlaitOperation, PlaitTheme, Viewport, Selection, ComponentType } from '@plait/core';
import { PlaitElement, PlaitOperation, PlaitTheme, Viewport, Selection, ComponentType, PlaitBoard } from '@plait/core';
import { RenderComponentRef } from '@plait/common';
import { ComponentRef } from '@angular/core';
import { BOARD_TO_COMPONENT } from '../utils/weak-maps';
import { BoardComponentInterface } from '../board/board.component.interface';

export interface AngularBoard {
renderComponent: <T, K extends { nativeElement: () => HTMLElement }>(
Expand All @@ -10,6 +12,13 @@ export interface AngularBoard {
) => { ref: RenderComponentRef<T>; componentRef: ComponentRef<K> };
}

export const AngularBoard = {
getBoardComponentInjector(board: PlaitBoard) {
const boardComponent = BOARD_TO_COMPONENT.get(board) as BoardComponentInterface;
return boardComponent.injector;
}
}

export interface OnChangeData {
children: PlaitElement[];
operations: PlaitOperation[];
Expand Down
18 changes: 13 additions & 5 deletions packages/common/src/utils/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import { Editor, Node, Element } from 'slate';
import { TextManage } from '../text/text-manage';
import { Alignment, CustomText, ParagraphElement } from '../text/types';

export interface TextInterface {
getTextEditors: (board: PlaitBoard, elements?: PlaitElement[]) => Editor[] | undefined;
findFirstTextEditor: (board: PlaitBoard) => null;
getFirstTextEditor: (element: PlaitElement) => Editor;
getTextEditorsByElement: (element: PlaitElement) => Editor[];
getEditingTextEditor: (board: PlaitBoard, elements?: PlaitElement[]) => Editor | undefined
}

export const getTextManages = (element: PlaitElement) => {
return ELEMENT_TO_TEXT_MANAGES.get(element) || [];
};
Expand All @@ -15,21 +23,21 @@ export const getFirstTextManage = (element: PlaitElement) => {
return textManage;
};

export const getTextEditorsByElement = (element: PlaitElement) => {
export const getTextEditorsByElement: TextInterface['getTextEditorsByElement'] = (element: PlaitElement) => {
return getTextManages(element).map(manage => {
return manage.editor;
});
};

export const getFirstTextEditor = (element: PlaitElement) => {
export const getFirstTextEditor: TextInterface['getFirstTextEditor'] = (element: PlaitElement) => {
const textEditor = getTextEditorsByElement(element)[0];
if (!textEditor) {
console.warn('can not find textManage');
}
return textEditor;
};

export const findFirstTextEditor = (board: PlaitBoard) => {
export const findFirstTextEditor: TextInterface['findFirstTextEditor'] = (board: PlaitBoard) => {
const selectedElements = getSelectedElements(board);
let firstEditor: Editor | null = null;
selectedElements.forEach(element => {
Expand Down Expand Up @@ -63,7 +71,7 @@ export const getElementsText = (elements: PlaitElement[]) => {
.join(' ');
};

export const getTextEditors = (board: PlaitBoard, elements?: PlaitElement[]) => {
export const getTextEditors: TextInterface['getTextEditors'] = (board: PlaitBoard, elements?: PlaitElement[]) => {
const selectedElements = elements || getSelectedElements(board);
if (selectedElements.length) {
const textManages: TextManage[] = [];
Expand All @@ -81,7 +89,7 @@ export const getTextEditors = (board: PlaitBoard, elements?: PlaitElement[]) =>
return undefined;
};

export const getEditingTextEditor = (board: PlaitBoard, elements?: PlaitElement[]) => {
export const getEditingTextEditor: TextInterface['getEditingTextEditor'] = (board: PlaitBoard, elements?: PlaitElement[]) => {
const selectedElements = elements || getSelectedElements(board);
const textManages: TextManage[] = [];
selectedElements.forEach(item => {
Expand Down
12 changes: 0 additions & 12 deletions packages/mind/src/emoji/emoji-base.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ export abstract class MindEmojiBaseComponent {

abstract nativeElement(): HTMLElement;

// TODO
// @HostListener('pointerdown')
// handlePointerDown() {
// const currentOptions = (this.board as PlaitOptionsBoard).getPluginOptions(PlaitPluginKey.withSelection);
// (this.board as PlaitOptionsBoard).setPluginOptions<WithPluginOptions>(PlaitPluginKey.withSelection, {
// isDisabledSelect: true
// });
// setTimeout(() => {
// (this.board as PlaitOptionsBoard).setPluginOptions<WithPluginOptions>(PlaitPluginKey.withSelection, { ...currentOptions });
// }, 0);
// }

initialize(): void {
this.nativeElement().style.fontSize = `${this.fontSize}px`;
this.nativeElement().classList.add('mind-node-emoji');
Expand Down
1 change: 1 addition & 0 deletions packages/text-plugins/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './clipboard';
export * from './common';
export * from './marks';
3 changes: 2 additions & 1 deletion packages/text-plugins/src/utils/marks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CustomText, getTextEditorsByElement, getTextManages } from '@plait/common';
import { PlaitElement } from '@plait/core';
import { PlaitMarkEditor } from '../mark/mark.editor';
import { Element } from 'slate';

export const getTextMarksByElement = (element: PlaitElement) => {
const editors = getTextEditorsByElement(element);
Expand All @@ -10,7 +11,7 @@ export const getTextMarksByElement = (element: PlaitElement) => {
}
if (editor.children.length === 0) {
const textManage = getTextManages(element)[0];
const currentMarks: Omit<CustomText, 'text'> = PlaitMarkEditor.getMarksByElement(textManage.componentRef.instance.children[0]);
const currentMarks: Omit<CustomText, 'text'> = PlaitMarkEditor.getMarksByElement(editor.children[0] as Element);
return currentMarks;
}
const currentMarks: Omit<CustomText, 'text'> = PlaitMarkEditor.getMarks(editor);
Expand Down

0 comments on commit be5c386

Please sign in to comment.