Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash while editing links on mobile #2363

Merged
merged 1 commit into from Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/editor/src/toolbar/tools/link.tsx
Expand Up @@ -105,6 +105,8 @@ export function EditLink(props: ToolProps) {

const onDone = useCallback(
(link: LinkDefinition) => {
if (!selectedNode.current) return;

const { href, text, isImage } = link;
const { from, node, to } = selectedNode.current;
if (!href || !editor.current || !node) return;
Expand All @@ -117,6 +119,7 @@ export function EditLink(props: ToolProps) {
let commandChain = editor.current.chain();

if (!isImage) {
console.log(from, to);
commandChain = commandChain.command(({ tr }) => {
tr.removeMark(from, to, mark.type);
tr.insertText(
Expand Down Expand Up @@ -161,6 +164,8 @@ export function EditLink(props: ToolProps) {
isEditing
onDone={onDone}
onClick={() => {
if (!selectedNode.current) return;

const { node } = selectedNode.current;
if (!node) return;

Expand Down Expand Up @@ -202,7 +207,7 @@ export function OpenLink(props: ToolProps) {
const selectedNode = useRefValue(
_selectedNode || selectionToOffset(editor.state)
);
const { node } = selectedNode.current;
const { node } = selectedNode.current || {};
const link = node ? findMark(node, "link") : null;
if (!link) return null;
const href = link?.attrs.href;
Expand Down
14 changes: 8 additions & 6 deletions packages/editor/src/toolbar/tools/web-clip.tsx
Expand Up @@ -46,9 +46,10 @@ export function WebClipFullScreen(props: ToolProps) {
{...props}
toggled={false}
onClick={() => {
const dom = editor.current?.view.nodeDOM(
selectionToOffset(editor.state).from
);
const offset = selectionToOffset(editor.state);
if (!offset) return;

const dom = editor.current?.view.nodeDOM(offset.from);
if (!dom || !(dom instanceof HTMLElement)) return;

const iframe = dom.querySelector("iframe");
Expand All @@ -70,9 +71,10 @@ export function WebClipOpenExternal(props: ToolProps) {
{...props}
toggled={false}
onClick={async () => {
const dom = editor.current?.view.nodeDOM(
selectionToOffset(editor.state).from
);
const offset = selectionToOffset(editor.state);
if (!offset) return;

const dom = editor.current?.view.nodeDOM(offset.from);
if (!dom || !(dom instanceof HTMLElement)) return;

const iframe = dom.querySelector("iframe");
Expand Down
15 changes: 10 additions & 5 deletions packages/editor/src/utils/prosemirror.ts
Expand Up @@ -120,12 +120,17 @@ export function findMark(
return mark;
}

export function selectionToOffset(state: EditorState): NodeWithOffset {
const { $from, from } = state.selection;
export function selectionToOffset(
state: EditorState
): NodeWithOffset | undefined {
const { from, $from } = state.selection;
const node = state.doc.nodeAt(from);
if (!node) return;

return {
node: state.doc.nodeAt(from) || undefined,
from,
to: from + $from.node().nodeSize
node,
from: from - $from.textOffset,
to: from - $from.textOffset + node.nodeSize
};
}

Expand Down