-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: make spanner plugin * feat: add config to spanner plugin * fix: linting errors * feat: config mountpoint * fix: unit test
- Loading branch information
1 parent
ec86a36
commit 805c9b8
Showing
11 changed files
with
164 additions
and
106 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, | ||
}; |
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
90 changes: 90 additions & 0 deletions
90
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,90 @@ | ||
import { BlockDataElement, Editor, IPlugin, PluginContext } from "../.."; | ||
import { take, takeUntil, fromEvent } from "rxjs"; | ||
import { SpannerDelegate, SpannerFactory } from "./spannerDelegate"; | ||
import { type Position } from "blocky-common/es"; | ||
|
||
const defaultWidth = 48; | ||
|
||
export interface SpannerPluginOptions { | ||
factory: SpannerFactory; | ||
width?: number; | ||
mountPoint?: HTMLElement; | ||
} | ||
|
||
export class SpannerPlugin implements IPlugin { | ||
deletage: SpannerDelegate | undefined; | ||
name = "spanner"; | ||
|
||
constructor(readonly options: SpannerPluginOptions) {} | ||
|
||
get container(): HTMLElement | undefined { | ||
return this.options.mountPoint ?? this.deletage?.editor.container; | ||
} | ||
|
||
onInitialized(context: PluginContext): void { | ||
const { editor, dispose$ } = context; | ||
this.deletage = new SpannerDelegate(editor, this.options.factory); | ||
|
||
const container = this.options.mountPoint ?? editor.container; | ||
this.deletage.mount(container); | ||
|
||
editor.placeSpannerAt$ | ||
.pipe(takeUntil(dispose$)) | ||
.subscribe(({ blockContainer, node }) => { | ||
this.placeSpannerAt(editor, blockContainer, node); | ||
}); | ||
|
||
fromEvent(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(blockContainer); | ||
const offset = block.getSpannerOffset(); | ||
x += offset.x; | ||
y += offset.y; | ||
x -= this.width; | ||
this.deletage.focusedNode = node; | ||
this.deletage.show(); | ||
this.deletage.setPosition(x, y); | ||
} | ||
|
||
get width(): number { | ||
return this.options.width ?? defaultWidth; | ||
} | ||
|
||
/** | ||
* Get the element's relative position to the container of the editor. | ||
*/ | ||
protected getRelativeOffsetByDom(element: HTMLElement): Position { | ||
const container = this.container; | ||
if (!container) { | ||
return { x: 0, y: 0 }; | ||
} | ||
const containerRect = 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.
805c9b8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
blocky-editor – ./
blocky-editor.vercel.app
blocky-editor-vincentdchan.vercel.app
www.blocky-editor.dev
blocky-editor-git-master-vincentdchan.vercel.app
blocky-editor.dev