Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(email): keep selected signature text when pasting a URL (#6074)

Cameron Pak33da70c0d010
diff --git a/apps/web/src/features/settings/SignatureEditor.tsx b/apps/web/src/features/settings/SignatureEditor.tsx
index 15dfa93aad..e35f73aac2 100644
--- a/apps/web/src/features/settings/SignatureEditor.tsx
+++ b/apps/web/src/features/settings/SignatureEditor.tsx
@@ -9,6 +9,7 @@ import {
import { Tooltip } from '@ui';
import Quill, { type Parchment } from 'quill';
import { createEffect, For, onCleanup, onMount } from 'solid-js';
+import { pastedHyperlinkHref } from './pastedHyperlinkHref';
import { setupImageResizer } from './signatureImageResizer';
// Quill's built-in font/size formats ship with hard whitelists (font:
@@ -202,6 +203,27 @@ export default function SignatureEditor(props: {
quill.on('text-change', () => props.onInput(currentHtml()));
teardownResizer = setupImageResizer(quill);
props.onReady?.({ setContent: (html) => setHtml(html) });
+
+ // Capture before Quill replaces the selection. A protocol URL on a range
+ // becomes a hyperlink on those words.
+ const onPaste = (event: ClipboardEvent) => {
+ if (!quill) return;
+ const range = quill.getSelection();
+ const href = pastedHyperlinkHref({
+ clipboardPlainText: event.clipboardData?.getData('text/plain') ?? '',
+ selectionCollapsed: range == null || range.length === 0,
+ selectionOps:
+ range == null
+ ? []
+ : (quill.getContents(range.index, range.length).ops ?? []),
+ });
+ if (href == null || range == null) return;
+ event.preventDefault();
+ event.stopPropagation();
+ quill.formatText(range.index, range.length, 'link', href, 'user');
+ };
+ quill.root.addEventListener('paste', onPaste, true);
+ onCleanup(() => quill?.root.removeEventListener('paste', onPaste, true));
});
// Reseed the editor when `props.value` changes. The draft store keeps
diff --git a/apps/web/src/features/settings/pastedHyperlinkHref.test.ts b/apps/web/src/features/settings/pastedHyperlinkHref.test.ts
new file mode 100644
index 0000000000..ded0fb3098
--- /dev/null
+++ b/apps/web/src/features/settings/pastedHyperlinkHref.test.ts
@@ -0,0 +1,166 @@
+import { describe, expect, it } from 'vitest';
+import { pastedHyperlinkHref } from './pastedHyperlinkHref.ts';
+
+const textSelection = [{ insert: 'word' }];
+
+describe('pastedHyperlinkHref', function () {
+ it('applies a selected https URL as the hyperlink href', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: 'https://lunchflow.app',
+ selectionCollapsed: false,
+ selectionOps: textSelection,
+ })
+ ).toBe('https://lunchflow.app');
+ });
+
+ it('applies nothing when the selection is collapsed', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: 'https://lunchflow.app',
+ selectionCollapsed: true,
+ selectionOps: textSelection,
+ })
+ ).toBeNull();
+ });
+
+ it('trims address-bar whitespace before applying the href', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: ' https://lunchflow.app \n',
+ selectionCollapsed: false,
+ selectionOps: textSelection,
+ })
+ ).toBe('https://lunchflow.app');
+ });
+
+ it('applies a selected http URL as the hyperlink href', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: 'http://lunchflow.app',
+ selectionCollapsed: false,
+ selectionOps: textSelection,
+ })
+ ).toBe('http://lunchflow.app');
+ });
+
+ it('applies an uppercase scheme', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: 'HTTPS://lunchflow.app',
+ selectionCollapsed: false,
+ selectionOps: textSelection,
+ })
+ ).toBe('HTTPS://lunchflow.app');
+ });
+
+ it('applies a URL with a path, query, and fragment', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: 'https://example.com/a?b=1#c',
+ selectionCollapsed: false,
+ selectionOps: textSelection,
+ })
+ ).toBe('https://example.com/a?b=1#c');
+ });
+
+ it('applies nothing for a bare host', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: 'example.com',
+ selectionCollapsed: false,
+ selectionOps: textSelection,
+ })
+ ).toBeNull();
+ });
+
+ it('applies nothing for a host without a scheme', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: 'Faith.tools',
+ selectionCollapsed: false,
+ selectionOps: textSelection,
+ })
+ ).toBeNull();
+ });
+
+ it('applies nothing when two URLs are separated by a newline', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: 'https://example.com\nhttps://other.com',
+ selectionCollapsed: false,
+ selectionOps: textSelection,
+ })
+ ).toBeNull();
+ });
+
+ it('applies nothing when the clipboard is a URL plus extra words', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: 'https://example.com extra words',
+ selectionCollapsed: false,
+ selectionOps: textSelection,
+ })
+ ).toBeNull();
+ });
+
+ it('applies nothing for a relative path', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: '/pricing',
+ selectionCollapsed: false,
+ selectionOps: textSelection,
+ })
+ ).toBeNull();
+ });
+
+ it('applies nothing for mailto or javascript schemes', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: 'mailto:[email protected]',
+ selectionCollapsed: false,
+ selectionOps: textSelection,
+ })
+ ).toBeNull();
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: 'javascript:alert(1)',
+ selectionCollapsed: false,
+ selectionOps: textSelection,
+ })
+ ).toBeNull();
+ });
+
+ it('applies nothing for empty clipboard text', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: '',
+ selectionCollapsed: false,
+ selectionOps: textSelection,
+ })
+ ).toBeNull();
+ });
+
+ it('applies nothing when the selection is an image embed', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: 'https://example.com',
+ selectionCollapsed: false,
+ selectionOps: [{ insert: { image: 'https://cdn.example/sig.png' } }],
+ })
+ ).toBeNull();
+ });
+
+ it('applies nothing when the selection mixes text and an image', function () {
+ expect(
+ pastedHyperlinkHref({
+ clipboardPlainText: 'https://example.com',
+ selectionCollapsed: false,
+ selectionOps: [
+ { insert: 'caption' },
+ { insert: { image: 'https://cdn.example/sig.png' } },
+ ],
+ })
+ ).toBeNull();
+ });
+});
diff --git a/apps/web/src/features/settings/pastedHyperlinkHref.ts b/apps/web/src/features/settings/pastedHyperlinkHref.ts
new file mode 100644
index 0000000000..3bc915dbc0
--- /dev/null
+++ b/apps/web/src/features/settings/pastedHyperlinkHref.ts
@@ -0,0 +1,25 @@
+export function pastedHyperlinkHref(args: {
+ clipboardPlainText: string;
+ selectionCollapsed: boolean;
+ selectionOps: ReadonlyArray<{ insert?: unknown }>;
+}): string | null {
+ if (args.selectionCollapsed) return null;
+ if (
+ args.selectionOps.length === 0 ||
+ args.selectionOps.some((op) => typeof op.insert !== 'string')
+ ) {
+ return null;
+ }
+ const href = args.clipboardPlainText.trim();
+ if (/\s/.test(href)) return null;
+ if (!/^https?:\/\//i.test(href)) return null;
+ try {
+ const parsed = new URL(href);
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
+ return null;
+ }
+ } catch {
+ return null;
+ }
+ return href;
+}