Compare commits

...

5 Commits

2 changed files with 62 additions and 13 deletions

View File

@@ -3,11 +3,8 @@ import {
EdgelessCRUDIdentifier, EdgelessCRUDIdentifier,
TextUtils, TextUtils,
} from '@blocksuite/affine-block-surface'; } from '@blocksuite/affine-block-surface';
import { import type { ShapeElementModel } from '@blocksuite/affine-model';
MindmapElementModel, import { MindmapElementModel, TextResizing } from '@blocksuite/affine-model';
ShapeElementModel,
TextResizing,
} from '@blocksuite/affine-model';
import type { RichText } from '@blocksuite/affine-rich-text'; import type { RichText } from '@blocksuite/affine-rich-text';
import { ThemeProvider } from '@blocksuite/affine-shared/services'; import { ThemeProvider } from '@blocksuite/affine-shared/services';
import { getSelectedRect } from '@blocksuite/affine-shared/utils'; import { getSelectedRect } from '@blocksuite/affine-shared/utils';
@@ -29,7 +26,7 @@ import { styleMap } from 'lit/directives/style-map.js';
import * as Y from 'yjs'; import * as Y from 'yjs';
export function mountShapeTextEditor( export function mountShapeTextEditor(
shapeElement: ShapeElementModel, shapeElement: { id: string; text?: Y.Text } | null | undefined,
edgeless: BlockComponent edgeless: BlockComponent
) { ) {
const mountElm = edgeless.querySelector('.edgeless-mount-point'); const mountElm = edgeless.querySelector('.edgeless-mount-point');
@@ -43,24 +40,27 @@ export function mountShapeTextEditor(
const gfx = edgeless.std.get(GfxControllerIdentifier); const gfx = edgeless.std.get(GfxControllerIdentifier);
const crud = edgeless.std.get(EdgelessCRUDIdentifier); const crud = edgeless.std.get(EdgelessCRUDIdentifier);
if (!shapeElement?.id) {
console.error('Cannot mount text editor on an invalid shape element');
return;
}
const updatedElement = crud.getElementById(shapeElement.id); const updatedElement = crud.getElementById(shapeElement.id);
if (!(updatedElement instanceof ShapeElementModel)) { if (!updatedElement || !('id' in updatedElement)) {
console.error('Cannot mount text editor on a non-shape element'); console.error('Cannot mount text editor on a non-shape element');
return; return;
} }
gfx.tool.setTool(DefaultTool); gfx.tool.setTool(DefaultTool);
gfx.selection.set({ gfx.selection.set({
elements: [shapeElement.id], elements: [updatedElement.id],
editing: true, editing: true,
}); });
if (!shapeElement.text) { if (!updatedElement.text) {
const text = new Y.Text(); const text = new Y.Text();
edgeless.std crud.updateElement(updatedElement.id, { text });
.get(EdgelessCRUDIdentifier)
.updateElement(shapeElement.id, { text });
} }
const shapeEditor = new EdgelessShapeTextEditor(); const shapeEditor = new EdgelessShapeTextEditor();
@@ -280,6 +280,21 @@ export class EdgelessShapeTextEditor extends WithDisposable(ShadowlessElement) {
this._unmount(); this._unmount();
} }
); );
this.disposables.addFromEvent(
this.inlineEditorContainer,
'compositionupdate',
() => {
this._updateElementWH();
}
);
this.disposables.addFromEvent(
this.inlineEditorContainer,
'compositionend',
() => {
this._updateElementWH();
}
);
}) })
.catch(console.error); .catch(console.error);

View File

@@ -1,8 +1,9 @@
import type { MindMapView } from '@blocksuite/affine/gfx/mindmap'; import type { MindMapView } from '@blocksuite/affine/gfx/mindmap';
import { mountShapeTextEditor } from '@blocksuite/affine/gfx/shape';
import { LayoutType, type MindmapElementModel } from '@blocksuite/affine-model'; import { LayoutType, type MindmapElementModel } from '@blocksuite/affine-model';
import { Bound } from '@blocksuite/global/gfx'; import { Bound } from '@blocksuite/global/gfx';
import type { GfxController } from '@blocksuite/std/gfx'; import type { GfxController } from '@blocksuite/std/gfx';
import { beforeEach, describe, expect, test } from 'vitest'; import { beforeEach, describe, expect, test, vi } from 'vitest';
import { click, pointermove, wait } from '../utils/common.js'; import { click, pointermove, wait } from '../utils/common.js';
import { getDocRootBlock } from '../utils/edgeless.js'; import { getDocRootBlock } from '../utils/edgeless.js';
@@ -36,6 +37,39 @@ describe('mindmap', () => {
return cleanup; return cleanup;
}); });
test('should update mindmap node editor size on compositionupdate', async () => {
const mindmapId = gfx.surface!.addElement({
type: 'mindmap',
children: {
text: 'root',
},
});
const mindmap = () => gfx.getElementById(mindmapId) as MindmapElementModel;
const root = getDocRootBlock(window.doc, window.editor, 'edgeless');
const rootNode = mindmap().tree.element;
mountShapeTextEditor(rootNode, root);
await wait();
const shapeEditor = root.querySelector('edgeless-shape-text-editor') as
| (HTMLElement & { inlineEditorContainer?: HTMLElement })
| null;
expect(shapeEditor).not.toBeNull();
const updateSpy = vi.spyOn(shapeEditor as any, '_updateElementWH');
const compositionUpdate = new CompositionEvent('compositionupdate', {
data: '拼',
bubbles: true,
});
shapeEditor!.inlineEditorContainer?.dispatchEvent(compositionUpdate);
expect(updateSpy).toHaveBeenCalled();
});
test('delete the root node should remove all children', async () => { test('delete the root node should remove all children', async () => {
const tree = { const tree = {
text: 'root', text: 'root',