Skip to content

Commit

Permalink
fix: mention embed
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentdchan committed Nov 25, 2023
1 parent 31f1fc5 commit 56b9102
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions packages/blocky-example/app/plugins/atPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,29 @@ import {
Delta,
} from "blocky-core";
import { makeReactFollowerWidget } from "blocky-react";
import { takeUntil } from "rxjs";
import { takeUntil, filter } from "rxjs";
import { get } from "lodash-es";
import "./atPanel.scss";

interface AtPanelProps {
closeWidget: () => void;
startOffset: number;
controller: EditorController;
}

const AtPanel = memo((props: AtPanelProps) => {
const { controller, closeWidget } = props;
const { startOffset, controller, closeWidget } = props;
const handleSelect = useCallback(() => {
controller.applyDeltaAtCursor((index) =>
const currentCursor = controller.state.cursorState;
if (!currentCursor?.isCollapsed) {
return;
}
const endIndex = currentCursor.endOffset;
const deleteLen = Math.max(endIndex - startOffset + 1, 1);
controller.applyDeltaAtCursor(() =>
new Delta()
.retain(Math.max(index - 1, 0))
.delete(1)
.retain(Math.max(startOffset - 1, 0))
.delete(deleteLen)
.insert({
type: "mention",
mention: "Vincent Chan",
Expand Down Expand Up @@ -59,12 +67,9 @@ class MyEmbed extends Embed {

constructor(props: EmbedInitOptions) {
super(props);
const content = get(props.record, ["mention"], "");
this.container.className = "blocky-mention";
this.container.textContent = "@Vincent";
}

dispose() {
console.log("delete mention");
this.container.textContent = "@" + content;
}
}

Expand All @@ -75,11 +80,11 @@ export function makeAtPanelPlugin(): IPlugin {
onInitialized(context: PluginContext) {
const { editor, dispose$ } = context;
editor.keyDown$
.pipe(takeUntil(dispose$))
.subscribe((e: KeyboardEvent) => {
if (e.key !== "@") {
return;
}
.pipe(
takeUntil(dispose$),
filter((e: KeyboardEvent) => e.key === "@")
)
.subscribe(() => {
editor.controller.enqueueNextTick(() => {
const blockElement = editor.controller.getBlockElementAtCursor();
if (!blockElement) {
Expand All @@ -88,9 +93,18 @@ export function makeAtPanelPlugin(): IPlugin {
if (blockElement.t !== TextBlock.Name) {
return;
}
const currentCursor = editor.controller.state.cursorState;
if (!currentCursor?.isCollapsed) {
return;
}
const startOffset = currentCursor.startOffset;
editor.insertFollowerWidget(
makeReactFollowerWidget(({ controller, closeWidget }) => (
<AtPanel controller={controller} closeWidget={closeWidget} />
<AtPanel
startOffset={startOffset}
controller={controller}
closeWidget={closeWidget}
/>
))
);
});
Expand Down

1 comment on commit 56b9102

@vercel
Copy link

@vercel vercel bot commented on 56b9102 Nov 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.