@vev/silke 1.1.10 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +162 -77
- package/CHANGELOG.md +41 -0
- package/dist/index.js +44 -41
- package/dist/index.js.LEGAL.txt +1 -1
- package/dist/index.js.map +7 -0
- package/dist/styles.css +1596 -1013
- package/dist/types/components/index.d.ts +2 -0
- package/dist/types/components/silke-autocomplete-field/autocomplete-item.d.ts +1 -1
- package/dist/types/components/silke-autocomplete-field/silke-autocomplete-field-native.d.ts +5 -0
- package/dist/types/components/silke-box/silke-box.d.ts +2 -0
- package/dist/types/components/silke-button/silke-dropdown-button.d.ts +3 -1
- package/dist/types/components/silke-card/silke-card-grid.d.ts +1 -0
- package/dist/types/components/silke-card/silke-card.d.ts +1 -0
- package/dist/types/components/silke-command-text-field/index.d.ts +1 -1
- package/dist/types/components/silke-command-text-field/silke-command-text-field.d.ts +16 -0
- package/dist/types/components/silke-command-text-field/utils.d.ts +25 -0
- package/dist/types/components/silke-css-number-field/silke-css-number-field.spec.d.ts +1 -0
- package/dist/types/components/silke-expression-input/index.d.ts +3 -0
- package/dist/types/components/silke-expression-input/silke-expression-input-tag.d.ts +6 -0
- package/dist/types/components/silke-expression-input/silke-expression-input.d.ts +29 -0
- package/dist/types/components/silke-expression-input/utils.d.ts +18 -0
- package/dist/types/components/silke-form-schema/fields/array-field-inline.d.ts +2 -1
- package/dist/types/components/silke-form-schema/fields/array-field-modal.d.ts +2 -1
- package/dist/types/components/silke-form-schema/fields/array-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/group-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/layout-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/object-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/popover-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/string-field.d.ts +1 -1
- package/dist/types/components/silke-form-schema/fields/utils/generate-preview.spec.d.ts +1 -0
- package/dist/types/components/silke-form-schema/fields/utils/generate-title.d.ts +10 -0
- package/dist/types/components/silke-form-schema/index.d.ts +1 -0
- package/dist/types/components/silke-form-schema/silke-form-schema.d.ts +4 -1
- package/dist/types/components/silke-form-schema/silke-schema-decorator.d.ts +17 -0
- package/dist/types/components/silke-form-schema/silke-schema-field.d.ts +2 -1
- package/dist/types/components/silke-form-schema/silke-schema-fields.d.ts +2 -0
- package/dist/types/components/silke-form-schema/silke-schema-path.spec.d.ts +1 -0
- package/dist/types/components/silke-gradient-picker/silke-gradient-utils.d.ts +2 -0
- package/dist/types/components/silke-icon-picker/color-swatch-section.d.ts +6 -0
- package/dist/types/components/silke-icon-picker/emoji-tab.d.ts +1 -1
- package/dist/types/components/silke-icon-picker/icons-tab.d.ts +1 -1
- package/dist/types/components/silke-icon-picker/silke-icon-picker.d.ts +2 -0
- package/dist/types/components/silke-icon-picker/utils.d.ts +2 -0
- package/dist/types/components/silke-list/silke-list-item.d.ts +5 -1
- package/dist/types/components/silke-list/types.d.ts +1 -0
- package/dist/types/components/silke-navigation-link/silke-navigation-link.d.ts +15 -0
- package/dist/types/components/silke-popover/popover-context.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/blockquote.d.ts +21 -0
- package/dist/types/components/silke-rich-text-field/blockquote.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/dropcap.d.ts +27 -0
- package/dist/types/components/silke-rich-text-field/dropcap.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/floating-layer.d.ts +12 -0
- package/dist/types/components/silke-rich-text-field/font-size.d.ts +2 -0
- package/dist/types/components/silke-rich-text-field/formatting-toolbar.d.ts +9 -0
- package/dist/types/components/silke-rich-text-field/index.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/jsdom-rects.d.ts +10 -0
- package/dist/types/components/silke-rich-text-field/link-menu.d.ts +26 -0
- package/dist/types/components/silke-rich-text-field/link-menu.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/link-suggestions.d.ts +48 -0
- package/dist/types/components/silke-rich-text-field/link-suggestions.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/link-tooltip.d.ts +15 -0
- package/dist/types/components/silke-rich-text-field/list-item.d.ts +15 -0
- package/dist/types/components/silke-rich-text-field/list-link-fontsize.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/lorem-ipsum.d.ts +10 -0
- package/dist/types/components/silke-rich-text-field/lorem-ipsum.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/paste.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/paste.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/plugins.d.ts +18 -0
- package/dist/types/components/silke-rich-text-field/plugins.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/silke-rich-text-field.d.ts +60 -0
- package/dist/types/components/silke-rich-text-field/slash-menu-items.d.ts +10 -0
- package/dist/types/components/silke-rich-text-field/slash-menu.d.ts +11 -0
- package/dist/types/components/silke-rich-text-field/table-content-sync.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/table-toolbar.d.ts +17 -0
- package/dist/types/components/silke-rich-text-field/table-toolbar.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/table.spec.d.ts +1 -0
- package/dist/types/components/silke-rich-text-field/use-link-hover.d.ts +15 -0
- package/dist/types/components/silke-rich-text-field/use-slash-menu.d.ts +13 -0
- package/dist/types/components/silke-rich-text-field/utils.d.ts +13 -0
- package/dist/types/components/silke-sortable-list/index.d.ts +1 -1
- package/dist/types/components/silke-sortable-list/silke-nested-sortable-list.d.ts +8 -1
- package/dist/types/components/silke-theme-card/silke-theme-card.d.ts +13 -6
- package/dist/types/components/silke-theme-swatch/silke-theme-swatch.d.ts +6 -1
- package/dist/types/icons.d.ts +16 -0
- package/esbuild.config.mjs +9 -4
- package/package.json +22 -9
- package/src/components/index.ts +2 -0
- package/src/components/silke-alert-banner/silke-alert-banner.tsx +1 -1
- package/src/components/silke-autocomplete-field/autocomplete-item.tsx +47 -61
- package/src/components/silke-autocomplete-field/silke-autocomplete-field-native.tsx +13 -2
- package/src/components/silke-autocomplete-field/silke-autocomplete-field.tsx +8 -5
- package/src/components/silke-autocomplete-field/silke-autocomplete-popup-native.tsx +2 -1
- package/src/components/silke-box/silke-box.scss +4 -0
- package/src/components/silke-box/silke-box.tsx +4 -0
- package/src/components/silke-button/silke-dropdown-button.tsx +4 -1
- package/src/components/silke-card/silke-card.scss +4 -0
- package/src/components/silke-card/silke-card.tsx +3 -0
- package/src/components/silke-command-text-field/index.ts +1 -0
- package/src/components/silke-command-text-field/silke-command-parser.spec.ts +55 -0
- package/src/components/silke-command-text-field/silke-command-parser.ts +33 -2
- package/src/components/silke-command-text-field/silke-command-text-field.module.scss +3 -0
- package/src/components/silke-command-text-field/silke-command-text-field.tsx +53 -48
- package/src/components/silke-command-text-field/utils.spec.ts +192 -0
- package/src/components/silke-command-text-field/utils.ts +99 -16
- package/src/components/silke-css-number-field/silke-css-number-field.spec.tsx +115 -0
- package/src/components/silke-css-number-field/silke-css-number-field.tsx +33 -0
- package/src/components/silke-expression-input/index.ts +3 -0
- package/src/components/silke-expression-input/silke-expression-input-tag.tsx +37 -0
- package/src/components/silke-expression-input/silke-expression-input.mdx +40 -0
- package/src/components/silke-expression-input/silke-expression-input.scss +24 -0
- package/src/components/silke-expression-input/silke-expression-input.tsx +343 -0
- package/src/components/silke-expression-input/utils.ts +144 -0
- package/src/components/silke-form-schema/fields/array-field-inline.tsx +4 -0
- package/src/components/silke-form-schema/fields/array-field-modal.tsx +4 -0
- package/src/components/silke-form-schema/fields/array-field.tsx +6 -11
- package/src/components/silke-form-schema/fields/group-field.tsx +2 -0
- package/src/components/silke-form-schema/fields/layout-field.tsx +2 -0
- package/src/components/silke-form-schema/fields/object-field.tsx +2 -0
- package/src/components/silke-form-schema/fields/popover-field.tsx +2 -0
- package/src/components/silke-form-schema/fields/string-field.tsx +33 -5
- package/src/components/silke-form-schema/fields/utils/generate-image.ts +5 -14
- package/src/components/silke-form-schema/fields/utils/generate-preview.spec.ts +49 -0
- package/src/components/silke-form-schema/fields/utils/generate-title.ts +23 -10
- package/src/components/silke-form-schema/index.ts +1 -0
- package/src/components/silke-form-schema/silke-form-schema.mdx +30 -1
- package/src/components/silke-form-schema/silke-form-schema.tsx +12 -6
- package/src/components/silke-form-schema/silke-schema-decorator.tsx +32 -0
- package/src/components/silke-form-schema/silke-schema-field.tsx +19 -2
- package/src/components/silke-form-schema/silke-schema-fields.tsx +30 -4
- package/src/components/silke-form-schema/silke-schema-path.spec.tsx +160 -0
- package/src/components/silke-gradient-picker/silke-gradient-picker.tsx +3 -1
- package/src/components/silke-gradient-picker/silke-gradient-utils.ts +23 -3
- package/src/components/silke-icon/silke-shape.tsx +1 -1
- package/src/components/silke-icon-picker/color-swatch-section.tsx +61 -0
- package/src/components/silke-icon-picker/emoji-tab.tsx +8 -6
- package/src/components/silke-icon-picker/icons-tab.tsx +8 -7
- package/src/components/silke-icon-picker/silke-icon-picker.tsx +52 -25
- package/src/components/silke-icon-picker/styles.scss +66 -1
- package/src/components/silke-icon-picker/utils.ts +2 -0
- package/src/components/silke-list/silke-list-item.scss +10 -0
- package/src/components/silke-list/silke-list-item.tsx +16 -3
- package/src/components/silke-list/silke-list.mdx +18 -0
- package/src/components/silke-list/types.ts +1 -0
- package/src/components/silke-navigation-link/silke-navigation-link.scss +162 -42
- package/src/components/silke-navigation-link/silke-navigation-link.tsx +99 -21
- package/src/components/silke-popover/popover-context.spec.tsx +44 -0
- package/src/components/silke-popover-native/silke-popover-native.scss +5 -3
- package/src/components/silke-rich-text-field/blockquote.spec.ts +151 -0
- package/src/components/silke-rich-text-field/blockquote.ts +90 -0
- package/src/components/silke-rich-text-field/dropcap.spec.ts +67 -0
- package/src/components/silke-rich-text-field/dropcap.ts +59 -0
- package/src/components/silke-rich-text-field/floating-layer.tsx +15 -0
- package/src/components/silke-rich-text-field/font-size.ts +62 -0
- package/src/components/silke-rich-text-field/formatting-toolbar.tsx +145 -0
- package/src/components/silke-rich-text-field/index.ts +1 -0
- package/src/components/silke-rich-text-field/jsdom-rects.ts +15 -0
- package/src/components/silke-rich-text-field/link-menu.spec.tsx +502 -0
- package/src/components/silke-rich-text-field/link-menu.tsx +553 -0
- package/src/components/silke-rich-text-field/link-suggestions.spec.ts +203 -0
- package/src/components/silke-rich-text-field/link-suggestions.ts +188 -0
- package/src/components/silke-rich-text-field/link-tooltip.tsx +60 -0
- package/src/components/silke-rich-text-field/list-item.ts +64 -0
- package/src/components/silke-rich-text-field/list-link-fontsize.spec.ts +135 -0
- package/src/components/silke-rich-text-field/lorem-ipsum.spec.ts +131 -0
- package/src/components/silke-rich-text-field/lorem-ipsum.ts +51 -0
- package/src/components/silke-rich-text-field/paste.spec.ts +93 -0
- package/src/components/silke-rich-text-field/paste.ts +39 -0
- package/src/components/silke-rich-text-field/plugins.spec.ts +143 -0
- package/src/components/silke-rich-text-field/plugins.ts +230 -0
- package/src/components/silke-rich-text-field/silke-rich-text-field.mdx +180 -0
- package/src/components/silke-rich-text-field/silke-rich-text-field.scss +343 -0
- package/src/components/silke-rich-text-field/silke-rich-text-field.tsx +505 -0
- package/src/components/silke-rich-text-field/slash-menu-items.ts +112 -0
- package/src/components/silke-rich-text-field/slash-menu.tsx +50 -0
- package/src/components/silke-rich-text-field/table-content-sync.spec.tsx +61 -0
- package/src/components/silke-rich-text-field/table-toolbar.spec.tsx +168 -0
- package/src/components/silke-rich-text-field/table-toolbar.tsx +121 -0
- package/src/components/silke-rich-text-field/table.spec.ts +87 -0
- package/src/components/silke-rich-text-field/use-link-hover.ts +63 -0
- package/src/components/silke-rich-text-field/use-slash-menu.ts +130 -0
- package/src/components/silke-rich-text-field/utils.ts +55 -0
- package/src/components/silke-sortable-list/index.ts +1 -1
- package/src/components/silke-sortable-list/silke-nested-sortable-list.scss +11 -1
- package/src/components/silke-sortable-list/silke-nested-sortable-list.tsx +9 -0
- package/src/components/silke-sortable-list/silke-sortable-list.mdx +13 -0
- package/src/components/silke-tag/silke-tag.scss +12 -3
- package/src/components/silke-theme-card/silke-theme-card.module.scss +141 -45
- package/src/components/silke-theme-card/silke-theme-card.tsx +76 -47
- package/src/components/silke-theme-swatch/silke-theme-swatch.mdx +56 -0
- package/src/components/silke-theme-swatch/silke-theme-swatch.module.scss +53 -63
- package/src/components/silke-theme-swatch/silke-theme-swatch.tsx +99 -14
- package/src/icons.tsx +51 -5
- package/tsconfig.build.tsbuildinfo +0 -1
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import classnames from 'classnames/bind';
|
|
3
|
+
import { SilkeBox } from '../silke-box';
|
|
4
|
+
import { SilkeButton } from '../silke-button';
|
|
5
|
+
import { SilkeTextField, SilkeTextFieldItem } from '../silke-text-field';
|
|
6
|
+
import { SilkeToggle } from '../silke-toggle';
|
|
7
|
+
import { SilkeText } from '../silke-text/silke-text';
|
|
8
|
+
import { SilkeDivider } from '../silke-divider';
|
|
9
|
+
import { SilkeListItem } from '../silke-list/silke-list-item';
|
|
10
|
+
import type { Entry, Data } from '../silke-autocomplete-field/autocomplete-types';
|
|
11
|
+
import styles from './silke-rich-text-field.scss';
|
|
12
|
+
import { SilkeTag } from '../silke-tag';
|
|
13
|
+
import { SilkeIcon, type SilkeIcons } from '../silke-icon';
|
|
14
|
+
import { useSilkeContext } from '../../silke-theme-provider';
|
|
15
|
+
import {
|
|
16
|
+
getLinkSuggestion,
|
|
17
|
+
normalizeHref,
|
|
18
|
+
parseMailto,
|
|
19
|
+
type LinkSuggestion,
|
|
20
|
+
} from './link-suggestions';
|
|
21
|
+
|
|
22
|
+
const cl = classnames.bind(styles);
|
|
23
|
+
|
|
24
|
+
const REL_OPTIONS = ['nofollow', 'noopener', 'noreferrer', 'sponsored', 'ugc'] as const;
|
|
25
|
+
|
|
26
|
+
export interface LinkAttributes {
|
|
27
|
+
class?: string | null;
|
|
28
|
+
/** Set for uploaded files: makes the browser save rather than navigate. */
|
|
29
|
+
download?: string | null;
|
|
30
|
+
/** Inline font-size on the <a> itself (plate / pasted HTML). See linkExtension. */
|
|
31
|
+
fontSize?: string | null;
|
|
32
|
+
href: string;
|
|
33
|
+
rel?: string | null;
|
|
34
|
+
target?: string | null;
|
|
35
|
+
title?: string | null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The link types you pick before you have anything to type. */
|
|
39
|
+
type StarterKind = 'email' | 'phone';
|
|
40
|
+
|
|
41
|
+
type LinkMenuRow =
|
|
42
|
+
| { kind: 'suggestion'; suggestion: LinkSuggestion }
|
|
43
|
+
| { kind: 'starter'; starter: StarterKind }
|
|
44
|
+
| { kind: 'upload' }
|
|
45
|
+
| { kind: 'back' }
|
|
46
|
+
| { kind: 'item'; item: Data<string> };
|
|
47
|
+
|
|
48
|
+
const STARTERS: Record<StarterKind, { label: string; icon: SilkeIcons; placeholder: string }> = {
|
|
49
|
+
email: { label: 'Email address…', icon: 'email', placeholder: 'name@example.com' },
|
|
50
|
+
phone: { label: 'Phone number…', icon: 'mobile', placeholder: '+47 987 65 432' },
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
interface LinkMenuProps {
|
|
54
|
+
items: Entry<string>[];
|
|
55
|
+
position: React.CSSProperties;
|
|
56
|
+
current?: LinkAttributes | null;
|
|
57
|
+
onSelect: (attrs: LinkAttributes) => void;
|
|
58
|
+
onUpdate?: (attrs: LinkAttributes) => void;
|
|
59
|
+
onAdd?: (value: string) => void;
|
|
60
|
+
onUnlink?: () => void;
|
|
61
|
+
onClose: () => void;
|
|
62
|
+
menuRef: React.RefObject<HTMLDivElement | null>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function LinkMenu({
|
|
66
|
+
items,
|
|
67
|
+
position,
|
|
68
|
+
current,
|
|
69
|
+
onSelect,
|
|
70
|
+
onUpdate,
|
|
71
|
+
onAdd,
|
|
72
|
+
onUnlink,
|
|
73
|
+
onClose,
|
|
74
|
+
menuRef,
|
|
75
|
+
}: LinkMenuProps) {
|
|
76
|
+
const dataItems = useMemo(
|
|
77
|
+
() => items.filter((item) => !item.divider && !item.disabled) as Data<string>[],
|
|
78
|
+
[items],
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
const matchedItem = useMemo(
|
|
82
|
+
() => (current?.href ? dataItems.find((item) => item.value === current.href) : undefined),
|
|
83
|
+
[current?.href, dataItems],
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// An existing mail link is split back apart, so the field shows the address and
|
|
87
|
+
// the subject lands in its own input instead of the raw encoded href.
|
|
88
|
+
const currentMail = useMemo(
|
|
89
|
+
() => (current?.href ? parseMailto(current.href) : null),
|
|
90
|
+
[current?.href],
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const [url, setUrl] = useState(
|
|
94
|
+
matchedItem?.label || currentMail?.email || current?.href || '',
|
|
95
|
+
);
|
|
96
|
+
const [showAdvanced, setShowAdvanced] = useState(false);
|
|
97
|
+
const [target, setTarget] = useState(current?.target || '');
|
|
98
|
+
const [rel, setRel] = useState(current?.rel || '');
|
|
99
|
+
const [subject, setSubject] = useState(currentMail?.subject || '');
|
|
100
|
+
// Set by the starter rows (and when reopening a mail link) so the input is read as
|
|
101
|
+
// that kind rather than re-guessed. Cleared again when the field is emptied.
|
|
102
|
+
const [pinned, setPinned] = useState<StarterKind | null>(currentMail?.email ? 'email' : null);
|
|
103
|
+
const [activeIndex, setActiveIndex] = useState(-1);
|
|
104
|
+
const listRef = useRef<HTMLUListElement>(null);
|
|
105
|
+
|
|
106
|
+
// SilkeTextField forwards its ref to the wrapper, not the input, so reach through it.
|
|
107
|
+
const fieldRef = useRef<HTMLDivElement>(null);
|
|
108
|
+
const focusField = useCallback(() => {
|
|
109
|
+
fieldRef.current?.querySelector('input')?.focus();
|
|
110
|
+
}, []);
|
|
111
|
+
|
|
112
|
+
/** Picking a link type should leave you able to just start typing. */
|
|
113
|
+
const pick = useCallback(
|
|
114
|
+
(starter: StarterKind) => {
|
|
115
|
+
setPinned(starter);
|
|
116
|
+
focusField();
|
|
117
|
+
},
|
|
118
|
+
[focusField],
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const { onFileUpload } = useSilkeContext();
|
|
122
|
+
const fileInputRef = useRef<HTMLInputElement>(null);
|
|
123
|
+
const [uploading, setUploading] = useState('');
|
|
124
|
+
const [uploadError, setUploadError] = useState('');
|
|
125
|
+
|
|
126
|
+
const relValues = rel ? rel.split(/\s+/).filter(Boolean) : [];
|
|
127
|
+
|
|
128
|
+
// If the value matches a known item's label, resolve it to that item's href.
|
|
129
|
+
const resolveHref = useCallback(
|
|
130
|
+
(value: string) => dataItems.find((item) => item.label === value)?.value || value,
|
|
131
|
+
[dataItems],
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The href to save for whatever is currently typed. resolveHref only changes the
|
|
136
|
+
* value when it matched a page/element label, so anything it hands back unchanged
|
|
137
|
+
* is free text and still needs normalizing (bare domain, email, phone number).
|
|
138
|
+
*/
|
|
139
|
+
const hrefForInput = useCallback(
|
|
140
|
+
(value: string) => {
|
|
141
|
+
const resolved = resolveHref(value);
|
|
142
|
+
if (resolved !== value) return resolved;
|
|
143
|
+
|
|
144
|
+
// Same interpretation the action row shows, so Enter and clicking agree.
|
|
145
|
+
const interpreted = getLinkSuggestion(value, { subject, kind: pinned || undefined });
|
|
146
|
+
return interpreted ? interpreted.href : normalizeHref(value);
|
|
147
|
+
},
|
|
148
|
+
[resolveHref, subject, pinned],
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
// When editing an existing link, apply target/rel changes immediately so they
|
|
152
|
+
// persist even if the menu is closed without re-submitting the URL. The just-set
|
|
153
|
+
// value is passed as an override, since the matching state update is async.
|
|
154
|
+
const applyIfEditing = useCallback(
|
|
155
|
+
(overrides: Partial<LinkAttributes>) => {
|
|
156
|
+
if (!current?.href) return;
|
|
157
|
+
const href = hrefForInput(url);
|
|
158
|
+
if (!href) return;
|
|
159
|
+
onUpdate?.({
|
|
160
|
+
download: current.download || null,
|
|
161
|
+
fontSize: current.fontSize || null,
|
|
162
|
+
href,
|
|
163
|
+
rel: rel || null,
|
|
164
|
+
target: target || null,
|
|
165
|
+
...overrides,
|
|
166
|
+
});
|
|
167
|
+
},
|
|
168
|
+
[current?.download, current?.fontSize, current?.href, hrefForInput, onUpdate, rel, target, url],
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
const toggleRel = (value: string) => {
|
|
172
|
+
const next = relValues.includes(value)
|
|
173
|
+
? relValues.filter((v) => v !== value)
|
|
174
|
+
: [...relValues, value];
|
|
175
|
+
const nextRel = next.join(' ');
|
|
176
|
+
setRel(nextRel);
|
|
177
|
+
applyIfEditing({ rel: nextRel || null });
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const searchLower = url.toLowerCase();
|
|
181
|
+
const filtered = useMemo(
|
|
182
|
+
() =>
|
|
183
|
+
searchLower
|
|
184
|
+
? dataItems.filter(
|
|
185
|
+
(item) =>
|
|
186
|
+
item.label?.toLowerCase().includes(searchLower) ||
|
|
187
|
+
item.value?.toLowerCase().includes(searchLower) ||
|
|
188
|
+
item.subLabel?.toLowerCase().includes(searchLower),
|
|
189
|
+
)
|
|
190
|
+
: dataItems,
|
|
191
|
+
[dataItems, searchLower],
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
// What the typed text means as an address — mail, phone, external or internal.
|
|
195
|
+
// Null for plain search text, where the page/element list is the answer.
|
|
196
|
+
const suggestion = useMemo(
|
|
197
|
+
() => getLinkSuggestion(url, { subject, kind: pinned || undefined }),
|
|
198
|
+
[url, subject, pinned],
|
|
199
|
+
);
|
|
200
|
+
const isMail = suggestion?.kind === 'email' || pinned === 'email';
|
|
201
|
+
|
|
202
|
+
// One list so arrow keys and the active highlight cover every row.
|
|
203
|
+
const rows = useMemo<LinkMenuRow[]>(() => {
|
|
204
|
+
const list: LinkMenuRow[] = [];
|
|
205
|
+
if (suggestion) list.push({ kind: 'suggestion', suggestion });
|
|
206
|
+
|
|
207
|
+
// The link types you would otherwise have to already know how to type. Shown
|
|
208
|
+
// only while browsing — once one is picked, the rest are noise.
|
|
209
|
+
if (!pinned && !url.trim()) {
|
|
210
|
+
list.push({ kind: 'starter', starter: 'email' });
|
|
211
|
+
list.push({ kind: 'starter', starter: 'phone' });
|
|
212
|
+
list.push({ kind: 'upload' });
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Pinned with nothing typed yet, the panel would otherwise be a dead end: the
|
|
216
|
+
// field is already empty, so there is nothing to clear to get back.
|
|
217
|
+
if (pinned && !url.trim()) list.push({ kind: 'back' });
|
|
218
|
+
|
|
219
|
+
// A pinned kind is not a page search, so page and element matches would be noise.
|
|
220
|
+
if (!pinned) for (const item of filtered) list.push({ kind: 'item', item });
|
|
221
|
+
return list;
|
|
222
|
+
}, [suggestion, pinned, url, filtered]);
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Closing the menu with a usable address in the field saves it, instead of throwing
|
|
226
|
+
* the typing away. Having to click the list row to commit was not discoverable —
|
|
227
|
+
* clicking anywhere else just lost the link.
|
|
228
|
+
*
|
|
229
|
+
* Only committed when the input resolves to a real destination, which is exactly
|
|
230
|
+
* when the action row is on screen. Plain search text ("Contact") is left alone, as
|
|
231
|
+
* are Escape and Remove link, which are cancellations.
|
|
232
|
+
*
|
|
233
|
+
* Kept in a ref refreshed every render so the unmount cleanup never sees stale
|
|
234
|
+
* state, and so it does not re-run on every keystroke.
|
|
235
|
+
*/
|
|
236
|
+
const commitOnCloseRef = useRef<() => void>(() => undefined);
|
|
237
|
+
commitOnCloseRef.current = () => {
|
|
238
|
+
if (settledRef.current || !suggestion) return;
|
|
239
|
+
if (suggestion.href === current?.href) return;
|
|
240
|
+
submit(suggestion.href);
|
|
241
|
+
};
|
|
242
|
+
useEffect(() => () => commitOnCloseRef.current(), []);
|
|
243
|
+
|
|
244
|
+
// Keep the keyboard-highlighted row inside the scroll viewport.
|
|
245
|
+
useEffect(() => {
|
|
246
|
+
if (activeIndex < 0) return;
|
|
247
|
+
listRef.current
|
|
248
|
+
?.querySelector(`[data-index="${activeIndex}"]`)
|
|
249
|
+
// Optional call: not every environment implements scrollIntoView.
|
|
250
|
+
?.scrollIntoView?.({ block: 'nearest' });
|
|
251
|
+
}, [activeIndex]);
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Set once the link has been dealt with — submitted, unlinked or cancelled — so the
|
|
255
|
+
* commit-on-close below does not fire a second time.
|
|
256
|
+
*/
|
|
257
|
+
const settledRef = useRef(false);
|
|
258
|
+
|
|
259
|
+
/** Takes a final href — callers run free text through hrefForInput first. */
|
|
260
|
+
const submit = useCallback(
|
|
261
|
+
(href: string, download?: string) => {
|
|
262
|
+
if (!href) return;
|
|
263
|
+
settledRef.current = true;
|
|
264
|
+
// Re-submitting an unchanged file link keeps its download attribute; pointing
|
|
265
|
+
// it somewhere else drops it, since only uploads are downloads.
|
|
266
|
+
const keep = href === current?.href ? current?.download : null;
|
|
267
|
+
onAdd?.(href);
|
|
268
|
+
onSelect({
|
|
269
|
+
href,
|
|
270
|
+
target: target || null,
|
|
271
|
+
rel: rel || null,
|
|
272
|
+
download: download || keep || null,
|
|
273
|
+
});
|
|
274
|
+
},
|
|
275
|
+
[onSelect, onAdd, target, rel, current?.href, current?.download],
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
const handleFile = useCallback(
|
|
279
|
+
async (file?: File | null) => {
|
|
280
|
+
if (!file) return;
|
|
281
|
+
setUploadError('');
|
|
282
|
+
setUploading(file.name);
|
|
283
|
+
try {
|
|
284
|
+
const fileUrl = await onFileUpload(file);
|
|
285
|
+
// `download` carries the original filename so the saved file keeps its name
|
|
286
|
+
// rather than the storage key.
|
|
287
|
+
submit(fileUrl, file.name);
|
|
288
|
+
} catch (e) {
|
|
289
|
+
console.error('Failed to upload file', file.name, e);
|
|
290
|
+
setUploadError('Upload failed, try again');
|
|
291
|
+
} finally {
|
|
292
|
+
setUploading('');
|
|
293
|
+
}
|
|
294
|
+
},
|
|
295
|
+
[onFileUpload, submit],
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
const handleKeyDown = (e: React.KeyboardEvent) => {
|
|
299
|
+
if (e.key === 'ArrowDown') {
|
|
300
|
+
e.preventDefault();
|
|
301
|
+
setActiveIndex((prev) => Math.min(prev + 1, rows.length - 1));
|
|
302
|
+
} else if (e.key === 'ArrowUp') {
|
|
303
|
+
e.preventDefault();
|
|
304
|
+
setActiveIndex((prev) => Math.max(prev - 1, -1));
|
|
305
|
+
} else if (e.key === 'Enter') {
|
|
306
|
+
e.preventDefault();
|
|
307
|
+
const row = activeIndex >= 0 ? rows[activeIndex] : undefined;
|
|
308
|
+
// With nothing highlighted, Enter does the same as the suggestion row would,
|
|
309
|
+
// so typing an address and hitting Enter always produces a working link.
|
|
310
|
+
if (row?.kind === 'suggestion') submit(row.suggestion.href);
|
|
311
|
+
else if (row?.kind === 'item') submit(row.item.value);
|
|
312
|
+
else if (row?.kind === 'starter') pick(row.starter);
|
|
313
|
+
else if (row?.kind === 'back') {
|
|
314
|
+
setPinned(null);
|
|
315
|
+
focusField();
|
|
316
|
+
}
|
|
317
|
+
else if (row?.kind === 'upload') fileInputRef.current?.click();
|
|
318
|
+
else if (url) submit(hrefForInput(url));
|
|
319
|
+
} else if (e.key === 'Escape') {
|
|
320
|
+
// A cancel, so nothing is committed on the way out.
|
|
321
|
+
settledRef.current = true;
|
|
322
|
+
onClose();
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
return (
|
|
327
|
+
<SilkeBox
|
|
328
|
+
data-rich-text-menu
|
|
329
|
+
data-cy="rich-text-link-menu"
|
|
330
|
+
ref={menuRef}
|
|
331
|
+
column
|
|
332
|
+
pad="xs"
|
|
333
|
+
gap="xs"
|
|
334
|
+
border
|
|
335
|
+
bg="neutral-10"
|
|
336
|
+
rounded="small"
|
|
337
|
+
shadow="level3"
|
|
338
|
+
className={cl('linkMenu')}
|
|
339
|
+
style={position}
|
|
340
|
+
onMouseDown={(e: React.MouseEvent) => {
|
|
341
|
+
// Prevent tiptap blur, but allow focus on interactive elements
|
|
342
|
+
const tag = (e.target as HTMLElement).tagName;
|
|
343
|
+
if (tag !== 'INPUT' && tag !== 'TEXTAREA') e.preventDefault();
|
|
344
|
+
}}
|
|
345
|
+
>
|
|
346
|
+
{/* Driven by the "Upload a file" row rather than shown directly — the popover
|
|
347
|
+
is too small for a dropzone, and the row keeps it in the list affordance. */}
|
|
348
|
+
<input
|
|
349
|
+
ref={fileInputRef}
|
|
350
|
+
type="file"
|
|
351
|
+
hidden
|
|
352
|
+
onChange={(e) => {
|
|
353
|
+
handleFile(e.target.files?.[0]);
|
|
354
|
+
// Reset so picking the same file twice still fires a change.
|
|
355
|
+
e.target.value = '';
|
|
356
|
+
}}
|
|
357
|
+
/>
|
|
358
|
+
<SilkeBox gap="xs" vAlign="center">
|
|
359
|
+
<SilkeTextField
|
|
360
|
+
ref={fieldRef}
|
|
361
|
+
data-cy="rich-text-link-field"
|
|
362
|
+
autoFocus
|
|
363
|
+
kind="ghost"
|
|
364
|
+
size="s"
|
|
365
|
+
flex
|
|
366
|
+
placeholder={pinned ? STARTERS[pinned].placeholder : 'Link, email, phone, or search pages'}
|
|
367
|
+
value={url}
|
|
368
|
+
onChange={(v) => {
|
|
369
|
+
const next = v as string;
|
|
370
|
+
setUrl(next);
|
|
371
|
+
setActiveIndex(-1);
|
|
372
|
+
// Clearing the field returns to browsing, so the starter rows come back.
|
|
373
|
+
if (!next.trim()) setPinned(null);
|
|
374
|
+
}}
|
|
375
|
+
onKeyDown={handleKeyDown}
|
|
376
|
+
>
|
|
377
|
+
{onUnlink && (
|
|
378
|
+
<SilkeTextFieldItem>
|
|
379
|
+
<SilkeButton
|
|
380
|
+
icon="unlink"
|
|
381
|
+
kind="ghost"
|
|
382
|
+
size="s"
|
|
383
|
+
title="Remove link"
|
|
384
|
+
onClick={() => {
|
|
385
|
+
settledRef.current = true;
|
|
386
|
+
onUnlink();
|
|
387
|
+
}}
|
|
388
|
+
/>
|
|
389
|
+
</SilkeTextFieldItem>
|
|
390
|
+
)}
|
|
391
|
+
</SilkeTextField>
|
|
392
|
+
</SilkeBox>
|
|
393
|
+
<SilkeDivider />
|
|
394
|
+
<SilkeBox vPad="xs" hPad="xs" gap="s" vAlign="center">
|
|
395
|
+
<SilkeBox flex gap="xs">
|
|
396
|
+
<SilkeToggle
|
|
397
|
+
small
|
|
398
|
+
value={target === '_blank'}
|
|
399
|
+
onChange={(v) => {
|
|
400
|
+
const nextTarget = v ? '_blank' : '';
|
|
401
|
+
setTarget(nextTarget);
|
|
402
|
+
applyIfEditing({ target: nextTarget || null });
|
|
403
|
+
}}
|
|
404
|
+
/>
|
|
405
|
+
<SilkeText size="small" weight="strong">
|
|
406
|
+
New Tab
|
|
407
|
+
</SilkeText>
|
|
408
|
+
</SilkeBox>
|
|
409
|
+
<SilkeBox
|
|
410
|
+
vPad="xxs"
|
|
411
|
+
hPad="xs"
|
|
412
|
+
rounded="tiny"
|
|
413
|
+
vAlign="center"
|
|
414
|
+
color="neutral-60"
|
|
415
|
+
bgHover="surface-2"
|
|
416
|
+
tag="button"
|
|
417
|
+
bg={showAdvanced ? 'surface-2' : 'surface-0'}
|
|
418
|
+
onClick={() => setShowAdvanced(!showAdvanced)}
|
|
419
|
+
>
|
|
420
|
+
<SilkeText size="small">Advanced</SilkeText>
|
|
421
|
+
<SilkeBox width="xs" height="xs" align="center">
|
|
422
|
+
<SilkeText size="small">
|
|
423
|
+
<SilkeIcon icon={showAdvanced ? 'chevron.up' : 'chevron.down'} />
|
|
424
|
+
</SilkeText>
|
|
425
|
+
</SilkeBox>
|
|
426
|
+
</SilkeBox>
|
|
427
|
+
</SilkeBox>
|
|
428
|
+
{showAdvanced && (
|
|
429
|
+
<>
|
|
430
|
+
<SilkeDivider />
|
|
431
|
+
{/* Contextual: a subject only means something for mail, rel only for web
|
|
432
|
+
links, so showing both at once would just be noise either way. */}
|
|
433
|
+
{isMail ? (
|
|
434
|
+
<SilkeBox column gap="s" vPad="xs" hPad="xs">
|
|
435
|
+
<SilkeText weight="strong" size="small" color="neutral-60">
|
|
436
|
+
Subject
|
|
437
|
+
</SilkeText>
|
|
438
|
+
<SilkeTextField
|
|
439
|
+
kind="default"
|
|
440
|
+
size="s"
|
|
441
|
+
placeholder="e.g. Super important email"
|
|
442
|
+
value={subject}
|
|
443
|
+
onChange={(v) => setSubject(v as string)}
|
|
444
|
+
onKeyDown={handleKeyDown}
|
|
445
|
+
onBlur={() => applyIfEditing({})}
|
|
446
|
+
/>
|
|
447
|
+
</SilkeBox>
|
|
448
|
+
) : (
|
|
449
|
+
<SilkeBox column gap="s" vPad="xs" hPad="xs">
|
|
450
|
+
<SilkeText weight="strong" size="small" color="neutral-60">
|
|
451
|
+
Rel attributes
|
|
452
|
+
</SilkeText>
|
|
453
|
+
<SilkeBox gap="s" wrap>
|
|
454
|
+
{REL_OPTIONS.map((option) => (
|
|
455
|
+
<SilkeTag
|
|
456
|
+
key={option}
|
|
457
|
+
label={option}
|
|
458
|
+
size="tiny"
|
|
459
|
+
color={relValues.includes(option) ? 'blue' : 'gray'}
|
|
460
|
+
onClick={() => toggleRel(option)}
|
|
461
|
+
/>
|
|
462
|
+
))}
|
|
463
|
+
</SilkeBox>
|
|
464
|
+
</SilkeBox>
|
|
465
|
+
)}
|
|
466
|
+
</>
|
|
467
|
+
)}
|
|
468
|
+
|
|
469
|
+
{rows.length > 0 && (
|
|
470
|
+
<>
|
|
471
|
+
<SilkeDivider />
|
|
472
|
+
<SilkeBox ref={listRef} tag="ul" column className={cl('linkMenuList')}>
|
|
473
|
+
{rows.map((row, index) => {
|
|
474
|
+
const previous = rows[index - 1];
|
|
475
|
+
const group = row.kind === 'item' ? row.item.groupLabel : undefined;
|
|
476
|
+
const showGroup =
|
|
477
|
+
row.kind === 'item' &&
|
|
478
|
+
row.item.groupLabel &&
|
|
479
|
+
(index === 0 ||
|
|
480
|
+
previous?.kind !== 'item' ||
|
|
481
|
+
previous.item.groupLabel !== row.item.groupLabel);
|
|
482
|
+
|
|
483
|
+
const rowProps =
|
|
484
|
+
row.kind === 'suggestion'
|
|
485
|
+
? {
|
|
486
|
+
label: row.suggestion.label,
|
|
487
|
+
icon: row.suggestion.icon,
|
|
488
|
+
onClick: () => submit(row.suggestion.href),
|
|
489
|
+
}
|
|
490
|
+
: row.kind === 'starter'
|
|
491
|
+
? {
|
|
492
|
+
label: STARTERS[row.starter].label,
|
|
493
|
+
icon: STARTERS[row.starter].icon,
|
|
494
|
+
onClick: () => pick(row.starter),
|
|
495
|
+
}
|
|
496
|
+
: row.kind === 'back'
|
|
497
|
+
? {
|
|
498
|
+
label: 'All link types',
|
|
499
|
+
icon: 'left' as const,
|
|
500
|
+
onClick: () => {
|
|
501
|
+
setPinned(null);
|
|
502
|
+
focusField();
|
|
503
|
+
},
|
|
504
|
+
}
|
|
505
|
+
: row.kind === 'upload'
|
|
506
|
+
? {
|
|
507
|
+
label: uploading ? `Uploading ${uploading}…` : 'Upload a file…',
|
|
508
|
+
subLabel: uploadError || undefined,
|
|
509
|
+
icon: 'file.download' as const,
|
|
510
|
+
disabled: !!uploading,
|
|
511
|
+
onClick: () => fileInputRef.current?.click(),
|
|
512
|
+
}
|
|
513
|
+
: {
|
|
514
|
+
label: row.item.label,
|
|
515
|
+
subLabel: row.item.subLabel,
|
|
516
|
+
icon: row.item.icon || 'link.external',
|
|
517
|
+
onClick: () => submit(row.item.value),
|
|
518
|
+
};
|
|
519
|
+
|
|
520
|
+
return (
|
|
521
|
+
<React.Fragment key={
|
|
522
|
+
row.kind === 'item'
|
|
523
|
+
? (row.item.value ?? index)
|
|
524
|
+
: row.kind === 'starter'
|
|
525
|
+
? `starter-${row.starter}`
|
|
526
|
+
: row.kind
|
|
527
|
+
}>
|
|
528
|
+
{showGroup && (
|
|
529
|
+
<SilkeBox tag="li" hPad="xs" vPad="xs" className={cl('linkMenuGroup')}>
|
|
530
|
+
<SilkeText size="small" color="neutral-60" weight="strong">
|
|
531
|
+
{group}
|
|
532
|
+
</SilkeText>
|
|
533
|
+
</SilkeBox>
|
|
534
|
+
)}
|
|
535
|
+
{/* Each item needs a row wrapper: SilkeListItem is `flex: 1`, which in a
|
|
536
|
+
column list would let the rows shrink below their own content height. */}
|
|
537
|
+
<SilkeBox tag="li" data-index={index}>
|
|
538
|
+
<SilkeListItem
|
|
539
|
+
{...rowProps}
|
|
540
|
+
size="s"
|
|
541
|
+
active={index === activeIndex}
|
|
542
|
+
onMouseEnter={() => setActiveIndex(index)}
|
|
543
|
+
/>
|
|
544
|
+
</SilkeBox>
|
|
545
|
+
</React.Fragment>
|
|
546
|
+
);
|
|
547
|
+
})}
|
|
548
|
+
</SilkeBox>
|
|
549
|
+
</>
|
|
550
|
+
)}
|
|
551
|
+
</SilkeBox>
|
|
552
|
+
);
|
|
553
|
+
}
|