-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #939 from gofractally/bf/chainmail-maintenance
- Loading branch information
Showing
7 changed files
with
246 additions
and
294 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
182 changes: 55 additions & 127 deletions
182
services/user/Chainmail/ui/src/components/editor/link-widget.tsx
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 |
---|---|---|
@@ -1,136 +1,64 @@ | ||
import { commandsCtx } from "@milkdown/core"; | ||
import { updateLinkCommand } from "@milkdown/preset-commonmark"; | ||
import { Plugin } from "@milkdown/prose/state"; | ||
import { DecorationSet } from "@milkdown/prose/view"; | ||
import { useInstance } from "@milkdown/react"; | ||
import { $prose } from "@milkdown/utils"; | ||
import type { useWidgetViewFactory } from "@prosemirror-adapter/react"; | ||
import { useWidgetViewContext } from "@prosemirror-adapter/react"; | ||
import type { Ctx } from "@milkdown/ctx"; | ||
import type { EditorView } from "@milkdown/prose/view"; | ||
import type { EditorState } from "@milkdown/prose/state"; | ||
|
||
export const LinkWidgetBefore = () => { | ||
return <>[</>; | ||
}; | ||
import { editorViewCtx } from "@milkdown/core"; | ||
import { linkSchema } from "@milkdown/preset-commonmark"; | ||
import { | ||
configureLinkTooltip, | ||
linkTooltipAPI, | ||
linkTooltipState, | ||
} from "@milkdown/components/link-tooltip"; | ||
import { TooltipProvider, tooltipFactory } from "@milkdown/plugin-tooltip"; | ||
|
||
export const LinkWidgetAfter = () => { | ||
const { spec } = useWidgetViewContext(); | ||
const [loading, editor] = useInstance(); | ||
const href = spec?.href ?? ""; | ||
const title = spec?.title ?? ""; | ||
import "../../styles/link-tooltip.css"; | ||
|
||
return ( | ||
<> | ||
] | ||
<span> | ||
( | ||
{ | ||
<> | ||
<small className="text-nord8 font-light">link: </small> | ||
<input | ||
size={href.length} | ||
placeholder="empty" | ||
onBlur={(e) => { | ||
if (loading) return; | ||
editor().action((ctx) => { | ||
const commands = ctx.get(commandsCtx); | ||
commands.call(updateLinkCommand.key, { | ||
href: e.target.value, | ||
}); | ||
}); | ||
}} | ||
className="rounded border-none bg-gray-50 px-2 py-0 ring-1 dark:bg-gray-900" | ||
type="text" | ||
defaultValue={href} | ||
/> | ||
| ||
<small className="text-nord8 font-light">title: </small> | ||
" | ||
<input | ||
size={title.length || 5} | ||
placeholder="empty" | ||
onBlur={(e) => { | ||
if (loading) return; | ||
editor().action((ctx) => { | ||
const commands = ctx.get(commandsCtx); | ||
commands.call(updateLinkCommand.key, { | ||
title: e.target.value, | ||
}); | ||
}); | ||
}} | ||
className="rounded border-none bg-gray-50 px-2 py-0 ring-1 dark:bg-gray-900" | ||
type="text" | ||
defaultValue={title} | ||
/> | ||
" | ||
</> | ||
} | ||
) | ||
</span> | ||
</> | ||
); | ||
}; | ||
export { linkTooltipPlugin } from "@milkdown/components/link-tooltip"; | ||
|
||
export const linkPlugin = ( | ||
widgetViewFactory: ReturnType<typeof useWidgetViewFactory>, | ||
) => { | ||
const before = widgetViewFactory({ | ||
as: "span", | ||
component: LinkWidgetBefore, | ||
}); | ||
const after = widgetViewFactory({ as: "span", component: LinkWidgetAfter }); | ||
|
||
return $prose( | ||
() => | ||
new Plugin({ | ||
state: { | ||
init() { | ||
return DecorationSet.empty; | ||
}, | ||
apply(tr) { | ||
const { selection } = tr; | ||
export const insertLinkTooltip = tooltipFactory("CREATE_LINK"); | ||
|
||
const { $from, $to } = selection; | ||
const node = tr.doc.nodeAt(selection.from); | ||
export function tooltipPluginView(ctx: Ctx) { | ||
return (_view: EditorView) => { | ||
const content = document.createElement("div"); | ||
const provider = new TooltipProvider({ | ||
content, | ||
shouldShow: (view: EditorView) => { | ||
const { selection, doc } = view.state; | ||
const has = doc.rangeHasMark( | ||
selection.from, | ||
selection.to, | ||
linkSchema.type(ctx), | ||
); | ||
if (has || selection.empty) return false; | ||
|
||
const mark = node?.marks.find( | ||
(mark) => mark.type.name === "link", | ||
); | ||
return true; | ||
}, | ||
}); | ||
|
||
if (!mark) return DecorationSet.empty; | ||
content.onmousedown = (e: MouseEvent) => { | ||
e.preventDefault(); | ||
const view = ctx.get(editorViewCtx); | ||
const { selection } = view.state; | ||
ctx.get(linkTooltipAPI.key).addLink(selection.from, selection.to); | ||
provider.hide(); | ||
}; | ||
|
||
let markPos = { start: -1, end: -1 }; | ||
tr.doc.nodesBetween( | ||
$from.start(), | ||
$to.end(), | ||
(n, pos) => { | ||
if (node === n) { | ||
markPos = { | ||
start: pos, | ||
end: | ||
pos + | ||
Math.max(n.textContent.length, 1), | ||
}; | ||
return { | ||
update: (updatedView: EditorView, prevState: EditorState) => { | ||
if (ctx.get(linkTooltipState.key).mode === "edit") return; | ||
provider.update(updatedView, prevState); | ||
}, | ||
destroy: () => { | ||
provider.destroy(); | ||
content.remove(); | ||
}, | ||
}; | ||
}; | ||
} | ||
|
||
// stop recursing if result is found | ||
return false; | ||
} | ||
return undefined; | ||
}, | ||
); | ||
|
||
return DecorationSet.create(tr.doc, [ | ||
before(markPos.start), | ||
after(markPos.end, { | ||
href: mark.attrs.href, | ||
title: mark.attrs.title, | ||
}), | ||
]); | ||
}, | ||
}, | ||
props: { | ||
decorations(state) { | ||
return this.getState(state); | ||
}, | ||
}, | ||
}), | ||
); | ||
}; | ||
export function linkTooltipConfig(ctx: Ctx) { | ||
ctx.set(insertLinkTooltip.key, { | ||
view: tooltipPluginView(ctx), | ||
}); | ||
configureLinkTooltip(ctx); | ||
} |
Oops, something went wrong.