Skip to content

Commit

Permalink
feat: remove path if empty after edit
Browse files Browse the repository at this point in the history
  • Loading branch information
F-star committed Feb 19, 2024
1 parent bde25ae commit cfe753d
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/core/src/path_editor/path_editor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { parseHexToRGBA } from '@suika/common';
import { getRotatedRectByTwoPoint, IPoint, isPointEqual } from '@suika/geo';

import { RemoveGraphsCmd } from '../commands';
import { ControlHandle } from '../control_handle_manager';
import { Editor } from '../editor';
import { Ellipse, Graph, Line, Path, Rect } from '../graphs';
Expand Down Expand Up @@ -31,12 +32,18 @@ export class PathEditor {
this.editor.sceneGraph.showSelectedGraphsOutline = false;
this.editor.sceneGraph.highlightLayersOnHover = false;

this.unbindHotkeys();
this.bindHotkeys();
this.editor.selectedElements.on('itemsChange', this.onSelectedChange);
// 监听
}
inactive() {
if (!this._active) {
return;
}

this._active = false;
this.removePathIfEmpty();

this.path = null;
this.editor.sceneGraph.showSelectedGraphsOutline = true;
this.editor.sceneGraph.highlightLayersOnHover = true;
Expand All @@ -45,10 +52,21 @@ export class PathEditor {
this.unbindHotkeys();
this.editor.selectedElements.off('itemsChange', this.onSelectedChange);
}

private removePathIfEmpty() {
if (
this.path &&
(this.path.pathData.length === 0 ||
this.path.pathData.every((item) => item.length <= 1))
) {
this.editor.commandManager.pushCommand(
new RemoveGraphsCmd('remove empty path', this.editor, [this.path]),
);
}
}
private bindHotkeys() {
const editor = this.editor;

this.eventTokens = [];
// delete / backspace: delete selected segments
const token = editor.keybindingManager.registerWithHighPrior({
key: [{ keyCode: 'Backspace' }, { keyCode: 'Delete' }],
Expand All @@ -69,6 +87,7 @@ export class PathEditor {
for (const token of this.eventTokens) {
this.editor.keybindingManager.unregister(token);
}
this.eventTokens = [];
}

/**
Expand Down

0 comments on commit cfe753d

Please sign in to comment.