From 3d6da26ad641f235bbe269ea9d74e174482e5566 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 4 Dec 2022 17:41:19 -0500 Subject: [PATCH] fix: Flash of external link decoration when creating doc from selected text --- app/editor/components/LinkToolbar.tsx | 3 ++- app/editor/components/SelectionToolbar.tsx | 3 ++- shared/utils/urls.ts | 7 ++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/editor/components/LinkToolbar.tsx b/app/editor/components/LinkToolbar.tsx index 554b11b8d..499a0a822 100644 --- a/app/editor/components/LinkToolbar.tsx +++ b/app/editor/components/LinkToolbar.tsx @@ -1,6 +1,7 @@ import { EditorView } from "prosemirror-view"; import * as React from "react"; import createAndInsertLink from "@shared/editor/commands/createAndInsertLink"; +import { creatingUrlPrefix } from "@shared/utils/urls"; import { Dictionary } from "~/hooks/useDictionary"; import FloatingToolbar from "./FloatingToolbar"; import LinkEditor, { SearchResult } from "./LinkEditor"; @@ -76,7 +77,7 @@ export default class LinkToolbar extends React.Component { return; } - const href = `creating#${title}…`; + const href = `${creatingUrlPrefix}${title}…`; // Insert a placeholder link dispatch( diff --git a/app/editor/components/SelectionToolbar.tsx b/app/editor/components/SelectionToolbar.tsx index bac98df29..bfe41deb3 100644 --- a/app/editor/components/SelectionToolbar.tsx +++ b/app/editor/components/SelectionToolbar.tsx @@ -12,6 +12,7 @@ import getRowIndex from "@shared/editor/queries/getRowIndex"; import isMarkActive from "@shared/editor/queries/isMarkActive"; import isNodeActive from "@shared/editor/queries/isNodeActive"; import { MenuItem } from "@shared/editor/types"; +import { creatingUrlPrefix } from "@shared/utils/urls"; import { Dictionary } from "~/hooks/useDictionary"; import getDividerMenuItems from "../menus/divider"; import getFormattingMenuItems from "../menus/formatting"; @@ -138,7 +139,7 @@ export default class SelectionToolbar extends React.Component { return; } - const href = `creating#${title}…`; + const href = `${creatingUrlPrefix}${title}…`; const markType = state.schema.marks.link; // Insert a placeholder link diff --git a/shared/utils/urls.ts b/shared/utils/urls.ts index 3c6e64f9c..06ade6aa4 100644 --- a/shared/utils/urls.ts +++ b/shared/utils/urls.ts @@ -75,6 +75,11 @@ export function isUrl(text: string, options?: { requireHostname: boolean }) { } } +/** + * Temporary prefix applied to links in document that are not yet persisted. + */ +export const creatingUrlPrefix = "creating#"; + /** * Returns true if the given string is a link to outside the application. * @@ -82,7 +87,7 @@ export function isUrl(text: string, options?: { requireHostname: boolean }) { * @returns True if the url is external, false otherwise. */ export function isExternalUrl(url: string) { - return !!url && !isInternalUrl(url); + return !!url && !isInternalUrl(url) && !url.startsWith(creatingUrlPrefix); } /**