-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec86a36
commit 6bbd6a6
Showing
11 changed files
with
133 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { SpannerPlugin } from "./spannerPlugin"; | ||
import { | ||
SpannerDelegate, | ||
type SpannerFactory, | ||
type SpannerInstance, | ||
} from "./spannerDelegate"; | ||
|
||
export { | ||
SpannerPlugin, | ||
SpannerDelegate, | ||
type SpannerFactory, | ||
type SpannerInstance, | ||
}; |
4 changes: 2 additions & 2 deletions
4
...cky-core/src/view/spannerDelegate.spec.ts → ...ins/spannerPlugin/spannerDelegate.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...s/blocky-core/src/view/spannerDelegate.ts → .../plugins/spannerPlugin/spannerDelegate.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/blocky-core/src/plugins/spannerPlugin/spannerPlugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { BlockDataElement, Editor, IPlugin, PluginContext } from "../.."; | ||
import { take, takeUntil, fromEvent } from "rxjs"; | ||
import { SpannerDelegate, SpannerFactory } from "./spannerDelegate"; | ||
|
||
export class SpannerPlugin implements IPlugin { | ||
deletage: SpannerDelegate | undefined; | ||
name = "spanner"; | ||
|
||
constructor(public readonly factory: SpannerFactory) {} | ||
|
||
onInitialized(context: PluginContext): void { | ||
const { editor, dispose$ } = context; | ||
this.deletage = new SpannerDelegate(editor.controller, this.factory); | ||
this.deletage.mount(editor.container); | ||
|
||
editor.placeSpannerAt$ | ||
.pipe(takeUntil(dispose$)) | ||
.subscribe(({ blockContainer, node }) => { | ||
this.placeSpannerAt(editor, blockContainer, node); | ||
}); | ||
|
||
fromEvent(editor.container, "mouseleave") | ||
.pipe(takeUntil(dispose$)) | ||
.subscribe(() => { | ||
this.deletage?.hide(); | ||
}); | ||
|
||
dispose$.pipe(take(1)).subscribe(() => { | ||
this.deletage?.dispose(); | ||
this.deletage = undefined; | ||
}); | ||
} | ||
|
||
protected placeSpannerAt( | ||
editor: Editor, | ||
blockContainer: HTMLElement, | ||
node: BlockDataElement | ||
) { | ||
if (!this.deletage) { | ||
return; | ||
} | ||
const block = editor.state.blocks.get(node.id); | ||
if (!block) { | ||
return; | ||
} | ||
let { x, y } = this.getRelativeOffsetByDom(editor, blockContainer); | ||
const offset = block.getSpannerOffset(); | ||
x += offset.x; | ||
y += offset.y; | ||
x -= this.deletage.width; | ||
this.deletage.focusedNode = node; | ||
this.deletage.show(); | ||
this.deletage.setPosition(x, y); | ||
} | ||
|
||
/** | ||
* Get the element's relative position to the container of the editor. | ||
*/ | ||
protected getRelativeOffsetByDom( | ||
editor: Editor, | ||
element: HTMLElement | ||
): { x: number; y: number } { | ||
const containerRect = editor.container.getBoundingClientRect(); | ||
const blockRect = element.getBoundingClientRect(); | ||
return { | ||
x: blockRect.x - containerRect.x, | ||
y: blockRect.y - containerRect.y, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.