@wordpress/format-library 4.16.0 → 4.17.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/CHANGELOG.md +2 -0
- package/build/bold/index.js +1 -10
- package/build/bold/index.js.map +1 -1
- package/build/code/index.js +3 -17
- package/build/code/index.js.map +1 -1
- package/build/default-formats.js +0 -14
- package/build/default-formats.js.map +1 -1
- package/build/default-formats.native.js +0 -6
- package/build/default-formats.native.js.map +1 -1
- package/build/image/index.js +5 -14
- package/build/image/index.js.map +1 -1
- package/build/index.js +1 -3
- package/build/index.js.map +1 -1
- package/build/italic/index.js +1 -10
- package/build/italic/index.js.map +1 -1
- package/build/keyboard/index.js +1 -10
- package/build/keyboard/index.js.map +1 -1
- package/build/language/index.js +1 -11
- package/build/language/index.js.map +1 -1
- package/build/link/index.js +5 -26
- package/build/link/index.js.map +1 -1
- package/build/link/index.native.js +14 -39
- package/build/link/index.native.js.map +1 -1
- package/build/link/inline.js +41 -78
- package/build/link/inline.js.map +1 -1
- package/build/link/modal-screens/link-picker-screen.native.js +1 -13
- package/build/link/modal-screens/link-picker-screen.native.js.map +1 -1
- package/build/link/modal-screens/link-settings-screen.native.js +9 -36
- package/build/link/modal-screens/link-settings-screen.native.js.map +1 -1
- package/build/link/modal-screens/screens.native.js.map +1 -1
- package/build/link/modal.native.js +1 -9
- package/build/link/modal.native.js.map +1 -1
- package/build/link/use-link-instance-key.js +3 -7
- package/build/link/use-link-instance-key.js.map +1 -1
- package/build/link/utils.js +23 -47
- package/build/link/utils.js.map +1 -1
- package/build/strikethrough/index.js +1 -9
- package/build/strikethrough/index.js.map +1 -1
- package/build/subscript/index.js +1 -10
- package/build/subscript/index.js.map +1 -1
- package/build/superscript/index.js +1 -10
- package/build/superscript/index.js.map +1 -1
- package/build/text-color/index.js +7 -25
- package/build/text-color/index.js.map +1 -1
- package/build/text-color/index.native.js +11 -34
- package/build/text-color/index.native.js.map +1 -1
- package/build/text-color/inline.js +5 -26
- package/build/text-color/inline.js.map +1 -1
- package/build/text-color/inline.native.js +9 -25
- package/build/text-color/inline.native.js.map +1 -1
- package/build/underline/index.js +1 -8
- package/build/underline/index.js.map +1 -1
- package/build/unknown/index.js +1 -11
- package/build/unknown/index.js.map +1 -1
- package/build-module/bold/index.js +0 -7
- package/build-module/bold/index.js.map +1 -1
- package/build-module/code/index.js +2 -14
- package/build-module/code/index.js.map +1 -1
- package/build-module/default-formats.js.map +1 -1
- package/build-module/default-formats.native.js.map +1 -1
- package/build-module/image/index.js +4 -10
- package/build-module/image/index.js.map +1 -1
- package/build-module/index.js +1 -1
- package/build-module/index.js.map +1 -1
- package/build-module/italic/index.js +0 -7
- package/build-module/italic/index.js.map +1 -1
- package/build-module/keyboard/index.js +0 -7
- package/build-module/keyboard/index.js.map +1 -1
- package/build-module/language/index.js +1 -8
- package/build-module/language/index.js.map +1 -1
- package/build-module/link/index.js +5 -17
- package/build-module/link/index.js.map +1 -1
- package/build-module/link/index.native.js +15 -30
- package/build-module/link/index.native.js.map +1 -1
- package/build-module/link/inline.js +42 -69
- package/build-module/link/inline.js.map +1 -1
- package/build-module/link/modal-screens/link-picker-screen.native.js +2 -8
- package/build-module/link/modal-screens/link-picker-screen.native.js.map +1 -1
- package/build-module/link/modal-screens/link-settings-screen.native.js +10 -28
- package/build-module/link/modal-screens/link-settings-screen.native.js.map +1 -1
- package/build-module/link/modal-screens/screens.native.js.map +1 -1
- package/build-module/link/modal.native.js +1 -4
- package/build-module/link/modal.native.js.map +1 -1
- package/build-module/link/use-link-instance-key.js +3 -7
- package/build-module/link/use-link-instance-key.js.map +1 -1
- package/build-module/link/utils.js +24 -43
- package/build-module/link/utils.js.map +1 -1
- package/build-module/strikethrough/index.js +0 -6
- package/build-module/strikethrough/index.js.map +1 -1
- package/build-module/subscript/index.js +0 -7
- package/build-module/subscript/index.js.map +1 -1
- package/build-module/superscript/index.js +0 -7
- package/build-module/superscript/index.js.map +1 -1
- package/build-module/text-color/index.js +7 -20
- package/build-module/text-color/index.js.map +1 -1
- package/build-module/text-color/index.native.js +12 -24
- package/build-module/text-color/index.native.js.map +1 -1
- package/build-module/text-color/inline.js +5 -20
- package/build-module/text-color/inline.js.map +1 -1
- package/build-module/text-color/inline.native.js +9 -21
- package/build-module/text-color/inline.native.js.map +1 -1
- package/build-module/underline/index.js +0 -6
- package/build-module/underline/index.js.map +1 -1
- package/build-module/unknown/index.js +0 -8
- package/build-module/unknown/index.js.map +1 -1
- package/package.json +13 -13
- package/src/link/inline.js +11 -34
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { createElement, Fragment } from "@wordpress/element";
|
|
2
|
-
|
|
3
2
|
/**
|
|
4
3
|
* WordPress dependencies
|
|
5
4
|
*/
|
|
@@ -11,16 +10,14 @@ import { RichTextToolbarButton, RichTextShortcut } from '@wordpress/block-editor
|
|
|
11
10
|
import { decodeEntities } from '@wordpress/html-entities';
|
|
12
11
|
import { link as linkIcon, linkOff } from '@wordpress/icons';
|
|
13
12
|
import { speak } from '@wordpress/a11y';
|
|
13
|
+
|
|
14
14
|
/**
|
|
15
15
|
* Internal dependencies
|
|
16
16
|
*/
|
|
17
|
-
|
|
18
17
|
import InlineLinkUI from './inline';
|
|
19
18
|
import { isValidHref } from './utils';
|
|
20
19
|
const name = 'core/link';
|
|
21
|
-
|
|
22
20
|
const title = __('Link');
|
|
23
|
-
|
|
24
21
|
function Edit({
|
|
25
22
|
isActive,
|
|
26
23
|
activeAttributes,
|
|
@@ -30,10 +27,8 @@ function Edit({
|
|
|
30
27
|
contentRef
|
|
31
28
|
}) {
|
|
32
29
|
const [addingLink, setAddingLink] = useState(false);
|
|
33
|
-
|
|
34
30
|
function addLink() {
|
|
35
31
|
const text = getTextContent(slice(value));
|
|
36
|
-
|
|
37
32
|
if (text && isURL(text) && isValidHref(text)) {
|
|
38
33
|
onChange(applyFormat(value, {
|
|
39
34
|
type: name,
|
|
@@ -52,20 +47,16 @@ function Edit({
|
|
|
52
47
|
setAddingLink(true);
|
|
53
48
|
}
|
|
54
49
|
}
|
|
55
|
-
|
|
56
50
|
function stopAddingLink(returnFocus = true) {
|
|
57
51
|
setAddingLink(false);
|
|
58
|
-
|
|
59
52
|
if (returnFocus) {
|
|
60
53
|
onFocus();
|
|
61
54
|
}
|
|
62
55
|
}
|
|
63
|
-
|
|
64
56
|
function onRemoveFormat() {
|
|
65
57
|
onChange(removeFormat(value, name));
|
|
66
58
|
speak(__('Link removed.'), 'assertive');
|
|
67
59
|
}
|
|
68
|
-
|
|
69
60
|
return createElement(Fragment, null, createElement(RichTextShortcut, {
|
|
70
61
|
type: "primary",
|
|
71
62
|
character: "k",
|
|
@@ -100,7 +91,6 @@ function Edit({
|
|
|
100
91
|
contentRef: contentRef
|
|
101
92
|
}));
|
|
102
93
|
}
|
|
103
|
-
|
|
104
94
|
export const link = {
|
|
105
95
|
name,
|
|
106
96
|
title,
|
|
@@ -112,7 +102,6 @@ export const link = {
|
|
|
112
102
|
id: 'data-id',
|
|
113
103
|
target: 'target'
|
|
114
104
|
},
|
|
115
|
-
|
|
116
105
|
__unstablePasteRule(value, {
|
|
117
106
|
html,
|
|
118
107
|
plainText
|
|
@@ -120,14 +109,14 @@ export const link = {
|
|
|
120
109
|
if (isCollapsed(value)) {
|
|
121
110
|
return value;
|
|
122
111
|
}
|
|
112
|
+
const pastedText = (html || plainText).replace(/<[^>]+>/g, '').trim();
|
|
123
113
|
|
|
124
|
-
|
|
125
|
-
|
|
114
|
+
// A URL was pasted, turn the selection into a link.
|
|
126
115
|
if (!isURL(pastedText)) {
|
|
127
116
|
return value;
|
|
128
|
-
}
|
|
129
|
-
|
|
117
|
+
}
|
|
130
118
|
|
|
119
|
+
// Allows us to ask for this information when we get a report.
|
|
131
120
|
window.console.log('Created link:\n\n', pastedText);
|
|
132
121
|
return applyFormat(value, {
|
|
133
122
|
type: name,
|
|
@@ -136,7 +125,6 @@ export const link = {
|
|
|
136
125
|
}
|
|
137
126
|
});
|
|
138
127
|
},
|
|
139
|
-
|
|
140
128
|
edit: Edit
|
|
141
129
|
};
|
|
142
130
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["__","useState","getTextContent","applyFormat","removeFormat","slice","isCollapsed","isURL","isEmail","RichTextToolbarButton","RichTextShortcut","decodeEntities","link","linkIcon","linkOff","speak","InlineLinkUI","isValidHref","name","title","Edit","isActive","activeAttributes","value","onChange","onFocus","contentRef","addingLink","setAddingLink","addLink","text","type","attributes","url","stopAddingLink","returnFocus","onRemoveFormat","createElement","Fragment","character","onUse","icon","onClick","shortcutType","shortcutCharacter","tagName","className","id","target","__unstablePasteRule","html","plainText","pastedText","replace","trim","window","console","log","edit"],"sources":["@wordpress/format-library/src/link/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport { useState } from '@wordpress/element';\nimport {\n\tgetTextContent,\n\tapplyFormat,\n\tremoveFormat,\n\tslice,\n\tisCollapsed,\n} from '@wordpress/rich-text';\nimport { isURL, isEmail } from '@wordpress/url';\nimport {\n\tRichTextToolbarButton,\n\tRichTextShortcut,\n} from '@wordpress/block-editor';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { link as linkIcon, linkOff } from '@wordpress/icons';\nimport { speak } from '@wordpress/a11y';\n\n/**\n * Internal dependencies\n */\nimport InlineLinkUI from './inline';\nimport { isValidHref } from './utils';\n\nconst name = 'core/link';\nconst title = __( 'Link' );\n\nfunction Edit( {\n\tisActive,\n\tactiveAttributes,\n\tvalue,\n\tonChange,\n\tonFocus,\n\tcontentRef,\n} ) {\n\tconst [ addingLink, setAddingLink ] = useState( false );\n\n\tfunction addLink() {\n\t\tconst text = getTextContent( slice( value ) );\n\n\t\tif ( text && isURL( text ) && isValidHref( text ) ) {\n\t\t\tonChange(\n\t\t\t\tapplyFormat( value, {\n\t\t\t\t\ttype: name,\n\t\t\t\t\tattributes: { url: text },\n\t\t\t\t} )\n\t\t\t);\n\t\t} else if ( text && isEmail( text ) ) {\n\t\t\tonChange(\n\t\t\t\tapplyFormat( value, {\n\t\t\t\t\ttype: name,\n\t\t\t\t\tattributes: { url: `mailto:${ text }` },\n\t\t\t\t} )\n\t\t\t);\n\t\t} else {\n\t\t\tsetAddingLink( true );\n\t\t}\n\t}\n\n\tfunction stopAddingLink( returnFocus = true ) {\n\t\tsetAddingLink( false );\n\t\tif ( returnFocus ) {\n\t\t\tonFocus();\n\t\t}\n\t}\n\n\tfunction onRemoveFormat() {\n\t\tonChange( removeFormat( value, name ) );\n\t\tspeak( __( 'Link removed.' ), 'assertive' );\n\t}\n\n\treturn (\n\t\t<>\n\t\t\t<RichTextShortcut type=\"primary\" character=\"k\" onUse={ addLink } />\n\t\t\t<RichTextShortcut\n\t\t\t\ttype=\"primaryShift\"\n\t\t\t\tcharacter=\"k\"\n\t\t\t\tonUse={ onRemoveFormat }\n\t\t\t/>\n\t\t\t{ isActive && (\n\t\t\t\t<RichTextToolbarButton\n\t\t\t\t\tname=\"link\"\n\t\t\t\t\ticon={ linkOff }\n\t\t\t\t\ttitle={ __( 'Unlink' ) }\n\t\t\t\t\tonClick={ onRemoveFormat }\n\t\t\t\t\tisActive={ isActive }\n\t\t\t\t\tshortcutType=\"primaryShift\"\n\t\t\t\t\tshortcutCharacter=\"k\"\n\t\t\t\t/>\n\t\t\t) }\n\t\t\t{ ! isActive && (\n\t\t\t\t<RichTextToolbarButton\n\t\t\t\t\tname=\"link\"\n\t\t\t\t\ticon={ linkIcon }\n\t\t\t\t\ttitle={ title }\n\t\t\t\t\tonClick={ addLink }\n\t\t\t\t\tisActive={ isActive }\n\t\t\t\t\tshortcutType=\"primary\"\n\t\t\t\t\tshortcutCharacter=\"k\"\n\t\t\t\t/>\n\t\t\t) }\n\t\t\t{ ( addingLink || isActive ) && (\n\t\t\t\t<InlineLinkUI\n\t\t\t\t\taddingLink={ addingLink }\n\t\t\t\t\tstopAddingLink={ stopAddingLink }\n\t\t\t\t\tisActive={ isActive }\n\t\t\t\t\tactiveAttributes={ activeAttributes }\n\t\t\t\t\tvalue={ value }\n\t\t\t\t\tonChange={ onChange }\n\t\t\t\t\tcontentRef={ contentRef }\n\t\t\t\t/>\n\t\t\t) }\n\t\t</>\n\t);\n}\n\nexport const link = {\n\tname,\n\ttitle,\n\ttagName: 'a',\n\tclassName: null,\n\tattributes: {\n\t\turl: 'href',\n\t\ttype: 'data-type',\n\t\tid: 'data-id',\n\t\ttarget: 'target',\n\t},\n\t__unstablePasteRule( value, { html, plainText } ) {\n\t\tif ( isCollapsed( value ) ) {\n\t\t\treturn value;\n\t\t}\n\n\t\tconst pastedText = ( html || plainText )\n\t\t\t.replace( /<[^>]+>/g, '' )\n\t\t\t.trim();\n\n\t\t// A URL was pasted, turn the selection into a link.\n\t\tif ( ! isURL( pastedText ) ) {\n\t\t\treturn value;\n\t\t}\n\n\t\t// Allows us to ask for this information when we get a report.\n\t\twindow.console.log( 'Created link:\\n\\n', pastedText );\n\n\t\treturn applyFormat( value, {\n\t\t\ttype: name,\n\t\t\tattributes: {\n\t\t\t\turl: decodeEntities( pastedText ),\n\t\t\t},\n\t\t} );\n\t},\n\tedit: Edit,\n};\n"],"mappings":";AAAA;AACA;AACA;AACA,SAASA,EAAE,QAAQ,iBAAiB;AACpC,SAASC,QAAQ,QAAQ,oBAAoB;AAC7C,SACCC,cAAc,EACdC,WAAW,EACXC,YAAY,EACZC,KAAK,EACLC,WAAW,QACL,sBAAsB;AAC7B,SAASC,KAAK,EAAEC,OAAO,QAAQ,gBAAgB;AAC/C,SACCC,qBAAqB,EACrBC,gBAAgB,QACV,yBAAyB;AAChC,SAASC,cAAc,QAAQ,0BAA0B;AACzD,SAASC,IAAI,IAAIC,QAAQ,EAAEC,OAAO,QAAQ,kBAAkB;AAC5D,SAASC,KAAK,QAAQ,iBAAiB;;AAEvC;AACA;AACA;AACA,OAAOC,YAAY,MAAM,UAAU;AACnC,SAASC,WAAW,QAAQ,SAAS;AA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AAE,EAAE,SAAS;IACbC,MAAM,EAAE;EACT,CAAC;EACDC,mBAAmBA,CAAE1B,KAAK,EAAE;IAAE2B,IAAI;IAAEC;EAAU,CAAC,EAAG;IACjD,IAAK7C,WAAW,CAAEiB,KAAM,CAAC,EAAG;MAC3B,OAAOA,KAAK;IACb;IAEA,MAAM6B,UAAU,GAAG,CAAEF,IAAI,IAAIC,SAAS,EACpCE,OAAO,CAAE,UAAU,EAAE,EAAG,CAAC,CACzBC,IAAI,CAAC,CAAC;;IAER;IACA,IAAK,CAAE/C,KAAK,CAAE6C,UAAW,CAAC,EAAG;MAC5B,OAAO7B,KAAK;IACb;;IAEA;IACAgC,MAAM,CAACC,OAAO,CAACC,GAAG,CAAE,mBAAmB,EAAEL,UAAW,CAAC;IAErD,OAAOjD,WAAW,CAAEoB,KAAK,EAAE;MAC1BQ,IAAI,EAAEb,IAAI;MACVc,UAAU,EAAE;QACXC,GAAG,EAAEtB,cAAc,CAAEyC,UAAW;MACjC;IACD,CAAE,CAAC;EACJ,CAAC;EACDM,IAAI,EAAEtC;AACP,CAAC"}
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { createElement, Fragment } from "@wordpress/element";
|
|
2
|
-
|
|
3
2
|
/**
|
|
4
3
|
* External dependencies
|
|
5
4
|
*/
|
|
6
5
|
import Clipboard from '@react-native-clipboard/clipboard';
|
|
6
|
+
|
|
7
7
|
/**
|
|
8
8
|
* WordPress dependencies
|
|
9
9
|
*/
|
|
10
|
-
|
|
11
10
|
import { __ } from '@wordpress/i18n';
|
|
12
11
|
import { Component } from '@wordpress/element';
|
|
13
12
|
import { withSpokenMessages } from '@wordpress/components';
|
|
@@ -15,10 +14,10 @@ import { RichTextToolbarButton } from '@wordpress/block-editor';
|
|
|
15
14
|
import { applyFormat, getActiveFormat, getTextContent, isCollapsed, removeFormat, slice } from '@wordpress/rich-text';
|
|
16
15
|
import { isURL } from '@wordpress/url';
|
|
17
16
|
import { link as linkIcon } from '@wordpress/icons';
|
|
17
|
+
|
|
18
18
|
/**
|
|
19
19
|
* Internal dependencies
|
|
20
20
|
*/
|
|
21
|
-
|
|
22
21
|
import ModalLinkUI from './modal';
|
|
23
22
|
import { isValidHref } from './utils';
|
|
24
23
|
const name = 'core/link';
|
|
@@ -43,14 +42,12 @@ export const link = {
|
|
|
43
42
|
addingLink: false
|
|
44
43
|
};
|
|
45
44
|
}
|
|
46
|
-
|
|
47
45
|
addLink() {
|
|
48
46
|
const {
|
|
49
47
|
value,
|
|
50
48
|
onChange
|
|
51
49
|
} = this.props;
|
|
52
50
|
const text = getTextContent(slice(value));
|
|
53
|
-
|
|
54
51
|
if (text && isURL(text) && isValidHref(text)) {
|
|
55
52
|
const newValue = applyFormat(value, {
|
|
56
53
|
type: name,
|
|
@@ -60,7 +57,8 @@ export const link = {
|
|
|
60
57
|
});
|
|
61
58
|
newValue.start = newValue.end;
|
|
62
59
|
newValue.activeFormats = [];
|
|
63
|
-
onChange({
|
|
60
|
+
onChange({
|
|
61
|
+
...newValue,
|
|
64
62
|
needsSelectionUpdate: true
|
|
65
63
|
});
|
|
66
64
|
} else {
|
|
@@ -70,91 +68,79 @@ export const link = {
|
|
|
70
68
|
this.getURLFromClipboard();
|
|
71
69
|
}
|
|
72
70
|
}
|
|
73
|
-
|
|
74
71
|
stopAddingLink() {
|
|
75
72
|
this.setState({
|
|
76
73
|
addingLink: false,
|
|
77
74
|
clipboardURL: undefined
|
|
78
75
|
});
|
|
79
76
|
}
|
|
80
|
-
|
|
81
77
|
getLinkSelection() {
|
|
82
78
|
const {
|
|
83
79
|
value,
|
|
84
80
|
isActive
|
|
85
81
|
} = this.props;
|
|
86
|
-
const startFormat = getActiveFormat(value, 'core/link');
|
|
87
|
-
// TODO: handle partly selected links.
|
|
82
|
+
const startFormat = getActiveFormat(value, 'core/link');
|
|
88
83
|
|
|
84
|
+
// If the link isn't selected, get the link manually by looking around the cursor
|
|
85
|
+
// TODO: handle partly selected links.
|
|
89
86
|
if (startFormat && isCollapsed(value) && isActive) {
|
|
90
87
|
let startIndex = value.start;
|
|
91
88
|
let endIndex = value.end;
|
|
92
|
-
|
|
93
89
|
while (value.formats[startIndex]?.find(format => format?.type === startFormat.type)) {
|
|
94
90
|
startIndex--;
|
|
95
91
|
}
|
|
96
|
-
|
|
97
92
|
endIndex++;
|
|
98
|
-
|
|
99
93
|
while (value.formats[endIndex]?.find(format => format?.type === startFormat.type)) {
|
|
100
94
|
endIndex++;
|
|
101
95
|
}
|
|
102
|
-
|
|
103
|
-
|
|
96
|
+
return {
|
|
97
|
+
...value,
|
|
104
98
|
start: startIndex + 1,
|
|
105
99
|
end: endIndex
|
|
106
100
|
};
|
|
107
101
|
}
|
|
108
|
-
|
|
109
102
|
return value;
|
|
110
103
|
}
|
|
111
|
-
|
|
112
104
|
onRemoveFormat() {
|
|
113
105
|
const {
|
|
114
106
|
onChange,
|
|
115
107
|
speak,
|
|
116
108
|
value
|
|
117
109
|
} = this.props;
|
|
118
|
-
const startFormat = getActiveFormat(value, 'core/link');
|
|
110
|
+
const startFormat = getActiveFormat(value, 'core/link');
|
|
119
111
|
|
|
112
|
+
// Before we try to remove anything we check if there is something at the caret position to remove.
|
|
120
113
|
if (isCollapsed(value) && startFormat === undefined) {
|
|
121
114
|
return;
|
|
122
115
|
}
|
|
123
|
-
|
|
124
116
|
const linkSelection = this.getLinkSelection();
|
|
125
117
|
onChange(removeFormat(linkSelection, name));
|
|
126
118
|
speak(__('Link removed.'), 'assertive');
|
|
127
119
|
}
|
|
128
|
-
|
|
129
120
|
async getURLFromClipboard() {
|
|
130
121
|
const clipboardText = await Clipboard.getString();
|
|
131
|
-
|
|
132
122
|
if (!clipboardText) {
|
|
133
123
|
return;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
|
|
124
|
+
}
|
|
125
|
+
// Check if pasted text is URL.
|
|
137
126
|
if (!isURL(clipboardText)) {
|
|
138
127
|
return;
|
|
139
128
|
}
|
|
140
|
-
|
|
141
129
|
this.setState({
|
|
142
130
|
clipboardURL: clipboardText
|
|
143
131
|
});
|
|
144
132
|
}
|
|
145
|
-
|
|
146
133
|
render() {
|
|
147
134
|
const {
|
|
148
135
|
isActive,
|
|
149
136
|
activeAttributes,
|
|
150
137
|
onChange
|
|
151
138
|
} = this.props;
|
|
152
|
-
const linkSelection = this.getLinkSelection();
|
|
153
|
-
|
|
139
|
+
const linkSelection = this.getLinkSelection();
|
|
140
|
+
// If no URL is set and we have a clipboard URL let's use it.
|
|
154
141
|
if (!activeAttributes.url && this.state.clipboardURL) {
|
|
155
142
|
activeAttributes.url = this.state.clipboardURL;
|
|
156
143
|
}
|
|
157
|
-
|
|
158
144
|
return createElement(Fragment, null, createElement(ModalLinkUI, {
|
|
159
145
|
isVisible: this.state.addingLink,
|
|
160
146
|
isActive: isActive,
|
|
@@ -173,7 +159,6 @@ export const link = {
|
|
|
173
159
|
shortcutCharacter: "k"
|
|
174
160
|
}));
|
|
175
161
|
}
|
|
176
|
-
|
|
177
162
|
})
|
|
178
163
|
};
|
|
179
164
|
//# sourceMappingURL=index.native.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/format-library/src/link/index.native.js"],"names":["Clipboard","__","Component","withSpokenMessages","RichTextToolbarButton","applyFormat","getActiveFormat","getTextContent","isCollapsed","removeFormat","slice","isURL","link","linkIcon","ModalLinkUI","isValidHref","name","title","tagName","className","attributes","url","target","rel","edit","LinkEdit","constructor","arguments","addLink","bind","stopAddingLink","onRemoveFormat","getURLFromClipboard","state","addingLink","value","onChange","props","text","newValue","type","start","end","activeFormats","needsSelectionUpdate","setState","clipboardURL","undefined","getLinkSelection","isActive","startFormat","startIndex","endIndex","formats","find","format","speak","linkSelection","clipboardText","getString","render","activeAttributes"],"mappings":";;AAAA;AACA;AACA;AACA,OAAOA,SAAP,MAAsB,mCAAtB;AAEA;AACA;AACA;;AACA,SAASC,EAAT,QAAmB,iBAAnB;AACA,SAASC,SAAT,QAA0B,oBAA1B;AACA,SAAS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A,MAAM,GAAG;AACR,YAAM;AAAEX,QAAAA,QAAF;AAAYY,QAAAA,gBAAZ;AAA8BzB,QAAAA;AAA9B,UAA2C,KAAKC,KAAtD;AACA,YAAMoB,aAAa,GAAG,KAAKT,gBAAL,EAAtB,CAFQ,CAGR;;AACA,UAAK,CAAEa,gBAAgB,CAACxC,GAAnB,IAA0B,KAAKY,KAAL,CAAWa,YAA1C,EAAyD;AACxDe,QAAAA,gBAAgB,CAACxC,GAAjB,GAAuB,KAAKY,KAAL,CAAWa,YAAlC;AACA;;AACD,aACC,8BACC,cAAC,WAAD;AACC,QAAA,SAAS,EAAG,KAAKb,KAAL,CAAWC,UADxB;AAEC,QAAA,QAAQ,EAAGe,QAFZ;AAGC,QAAA,gBAAgB,EAAGY,gBAHpB;AAIC,QAAA,OAAO,EAAG,KAAK/B,cAJhB;AAKC,QAAA,QAAQ,EAAGM,QALZ;AAMC,QAAA,QAAQ,EAAG,KAAKL,cANjB;AAOC,QAAA,KAAK,EAAG0B;AAPT,QADD,EAUC,cAAC,qBAAD;AACC,QAAA,IAAI,EAAC,MADN;AAEC,QAAA,IAAI,EAAG5C,QAFR;AAGC,QAAA,KAAK,EAAGZ,EAAE,CAAE,MAAF,CAHX;AAIC,QAAA,OAAO,EAAG,KAAK2B,OAJhB;AAKC,QAAA,QAAQ,EAAGqB,QALZ;AAMC,QAAA,YAAY,EAAC,SANd;AAOC,QAAA,iBAAiB,EAAC;AAPnB,QAVD,CADD;AAsBA;;AAlI+B,GADV;AAVL,CAAb","sourcesContent":["/**\n * External dependencies\n */\nimport Clipboard from '@react-native-clipboard/clipboard';\n\n/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport { Component } from '@wordpress/element';\nimport { withSpokenMessages } from '@wordpress/components';\nimport { RichTextToolbarButton } from '@wordpress/block-editor';\nimport {\n\tapplyFormat,\n\tgetActiveFormat,\n\tgetTextContent,\n\tisCollapsed,\n\tremoveFormat,\n\tslice,\n} from '@wordpress/rich-text';\nimport { isURL } from '@wordpress/url';\nimport { link as linkIcon } from '@wordpress/icons';\n\n/**\n * Internal dependencies\n */\nimport ModalLinkUI from './modal';\nimport { isValidHref } from './utils';\n\nconst name = 'core/link';\n\nexport const link = {\n\tname,\n\ttitle: __( 'Link' ),\n\ttagName: 'a',\n\tclassName: null,\n\tattributes: {\n\t\turl: 'href',\n\t\ttarget: 'target',\n\t\trel: 'rel',\n\t},\n\tedit: withSpokenMessages(\n\t\tclass LinkEdit extends Component {\n\t\t\tconstructor() {\n\t\t\t\tsuper( ...arguments );\n\n\t\t\t\tthis.addLink = this.addLink.bind( this );\n\t\t\t\tthis.stopAddingLink = this.stopAddingLink.bind( this );\n\t\t\t\tthis.onRemoveFormat = this.onRemoveFormat.bind( this );\n\t\t\t\tthis.getURLFromClipboard =\n\t\t\t\t\tthis.getURLFromClipboard.bind( this );\n\t\t\t\tthis.state = {\n\t\t\t\t\taddingLink: false,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\taddLink() {\n\t\t\t\tconst { value, onChange } = this.props;\n\t\t\t\tconst text = getTextContent( slice( value ) );\n\n\t\t\t\tif ( text && isURL( text ) && isValidHref( text ) ) {\n\t\t\t\t\tconst newValue = applyFormat( value, {\n\t\t\t\t\t\ttype: name,\n\t\t\t\t\t\tattributes: { url: text },\n\t\t\t\t\t} );\n\t\t\t\t\tnewValue.start = newValue.end;\n\t\t\t\t\tnewValue.activeFormats = [];\n\t\t\t\t\tonChange( { ...newValue, needsSelectionUpdate: true } );\n\t\t\t\t} else {\n\t\t\t\t\tthis.setState( { addingLink: true } );\n\t\t\t\t\tthis.getURLFromClipboard();\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tstopAddingLink() {\n\t\t\t\tthis.setState( { addingLink: false, clipboardURL: undefined } );\n\t\t\t}\n\n\t\t\tgetLinkSelection() {\n\t\t\t\tconst { value, isActive } = this.props;\n\t\t\t\tconst startFormat = getActiveFormat( value, 'core/link' );\n\n\t\t\t\t// If the link isn't selected, get the link manually by looking around the cursor\n\t\t\t\t// TODO: handle partly selected links.\n\t\t\t\tif ( startFormat && isCollapsed( value ) && isActive ) {\n\t\t\t\t\tlet startIndex = value.start;\n\t\t\t\t\tlet endIndex = value.end;\n\n\t\t\t\t\twhile (\n\t\t\t\t\t\tvalue.formats[ startIndex ]?.find(\n\t\t\t\t\t\t\t( format ) => format?.type === startFormat.type\n\t\t\t\t\t\t)\n\t\t\t\t\t) {\n\t\t\t\t\t\tstartIndex--;\n\t\t\t\t\t}\n\n\t\t\t\t\tendIndex++;\n\n\t\t\t\t\twhile (\n\t\t\t\t\t\tvalue.formats[ endIndex ]?.find(\n\t\t\t\t\t\t\t( format ) => format?.type === startFormat.type\n\t\t\t\t\t\t)\n\t\t\t\t\t) {\n\t\t\t\t\t\tendIndex++;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...value,\n\t\t\t\t\t\tstart: startIndex + 1,\n\t\t\t\t\t\tend: endIndex,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn value;\n\t\t\t}\n\n\t\t\tonRemoveFormat() {\n\t\t\t\tconst { onChange, speak, value } = this.props;\n\t\t\t\tconst startFormat = getActiveFormat( value, 'core/link' );\n\n\t\t\t\t// Before we try to remove anything we check if there is something at the caret position to remove.\n\t\t\t\tif ( isCollapsed( value ) && startFormat === undefined ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tconst linkSelection = this.getLinkSelection();\n\n\t\t\t\tonChange( removeFormat( linkSelection, name ) );\n\t\t\t\tspeak( __( 'Link removed.' ), 'assertive' );\n\t\t\t}\n\n\t\t\tasync getURLFromClipboard() {\n\t\t\t\tconst clipboardText = await Clipboard.getString();\n\t\t\t\tif ( ! clipboardText ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\t// Check if pasted text is URL.\n\t\t\t\tif ( ! isURL( clipboardText ) ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tthis.setState( { clipboardURL: clipboardText } );\n\t\t\t}\n\n\t\t\trender() {\n\t\t\t\tconst { isActive, activeAttributes, onChange } = this.props;\n\t\t\t\tconst linkSelection = this.getLinkSelection();\n\t\t\t\t// If no URL is set and we have a clipboard URL let's use it.\n\t\t\t\tif ( ! activeAttributes.url && this.state.clipboardURL ) {\n\t\t\t\t\tactiveAttributes.url = this.state.clipboardURL;\n\t\t\t\t}\n\t\t\t\treturn (\n\t\t\t\t\t<>\n\t\t\t\t\t\t<ModalLinkUI\n\t\t\t\t\t\t\tisVisible={ this.state.addingLink }\n\t\t\t\t\t\t\tisActive={ isActive }\n\t\t\t\t\t\t\tactiveAttributes={ activeAttributes }\n\t\t\t\t\t\t\tonClose={ this.stopAddingLink }\n\t\t\t\t\t\t\tonChange={ onChange }\n\t\t\t\t\t\t\tonRemove={ this.onRemoveFormat }\n\t\t\t\t\t\t\tvalue={ linkSelection }\n\t\t\t\t\t\t/>\n\t\t\t\t\t\t<RichTextToolbarButton\n\t\t\t\t\t\t\tname=\"link\"\n\t\t\t\t\t\t\ticon={ linkIcon }\n\t\t\t\t\t\t\ttitle={ __( 'Link' ) }\n\t\t\t\t\t\t\tonClick={ this.addLink }\n\t\t\t\t\t\t\tisActive={ isActive }\n\t\t\t\t\t\t\tshortcutType=\"primary\"\n\t\t\t\t\t\t\tshortcutCharacter=\"k\"\n\t\t\t\t\t\t/>\n\t\t\t\t\t</>\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t),\n};\n"]}
|
|
1
|
+
{"version":3,"names":["Clipboard","__","Component","withSpokenMessages","RichTextToolbarButton","applyFormat","getActiveFormat","getTextContent","isCollapsed","removeFormat","slice","isURL","link","linkIcon","ModalLinkUI","isValidHref","name","title","tagName","className","attributes","url","target","rel","edit","LinkEdit","constructor","arguments","addLink","bind","stopAddingLink","onRemoveFormat","getURLFromClipboard","state","addingLink","value","onChange","props","text","newValue","type","start","end","activeFormats","needsSelectionUpdate","setState","clipboardURL","undefined","getLinkSelection","isActive","startFormat","startIndex","endIndex","formats","find","format","speak","linkSelection","clipboardText","getString","render","activeAttributes","createElement","Fragment","isVisible","onClose","onRemove","icon","onClick","shortcutType","shortcutCharacter"],"sources":["@wordpress/format-library/src/link/index.native.js"],"sourcesContent":["/**\n * External dependencies\n */\nimport Clipboard from '@react-native-clipboard/clipboard';\n\n/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport { Component } from '@wordpress/element';\nimport { withSpokenMessages } from '@wordpress/components';\nimport { RichTextToolbarButton } from '@wordpress/block-editor';\nimport {\n\tapplyFormat,\n\tgetActiveFormat,\n\tgetTextContent,\n\tisCollapsed,\n\tremoveFormat,\n\tslice,\n} from '@wordpress/rich-text';\nimport { isURL } from '@wordpress/url';\nimport { link as linkIcon } from '@wordpress/icons';\n\n/**\n * Internal dependencies\n */\nimport ModalLinkUI from './modal';\nimport { isValidHref } from './utils';\n\nconst name = 'core/link';\n\nexport const link = {\n\tname,\n\ttitle: __( 'Link' ),\n\ttagName: 'a',\n\tclassName: null,\n\tattributes: {\n\t\turl: 'href',\n\t\ttarget: 'target',\n\t\trel: 'rel',\n\t},\n\tedit: withSpokenMessages(\n\t\tclass LinkEdit extends Component {\n\t\t\tconstructor() {\n\t\t\t\tsuper( ...arguments );\n\n\t\t\t\tthis.addLink = this.addLink.bind( this );\n\t\t\t\tthis.stopAddingLink = this.stopAddingLink.bind( this );\n\t\t\t\tthis.onRemoveFormat = this.onRemoveFormat.bind( this );\n\t\t\t\tthis.getURLFromClipboard =\n\t\t\t\t\tthis.getURLFromClipboard.bind( this );\n\t\t\t\tthis.state = {\n\t\t\t\t\taddingLink: false,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\taddLink() {\n\t\t\t\tconst { value, onChange } = this.props;\n\t\t\t\tconst text = getTextContent( slice( value ) );\n\n\t\t\t\tif ( text && isURL( text ) && isValidHref( text ) ) {\n\t\t\t\t\tconst newValue = applyFormat( value, {\n\t\t\t\t\t\ttype: name,\n\t\t\t\t\t\tattributes: { url: text },\n\t\t\t\t\t} );\n\t\t\t\t\tnewValue.start = newValue.end;\n\t\t\t\t\tnewValue.activeFormats = [];\n\t\t\t\t\tonChange( { ...newValue, needsSelectionUpdate: true } );\n\t\t\t\t} else {\n\t\t\t\t\tthis.setState( { addingLink: true } );\n\t\t\t\t\tthis.getURLFromClipboard();\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tstopAddingLink() {\n\t\t\t\tthis.setState( { addingLink: false, clipboardURL: undefined } );\n\t\t\t}\n\n\t\t\tgetLinkSelection() {\n\t\t\t\tconst { value, isActive } = this.props;\n\t\t\t\tconst startFormat = getActiveFormat( value, 'core/link' );\n\n\t\t\t\t// If the link isn't selected, get the link manually by looking around the cursor\n\t\t\t\t// TODO: handle partly selected links.\n\t\t\t\tif ( startFormat && isCollapsed( value ) && isActive ) {\n\t\t\t\t\tlet startIndex = value.start;\n\t\t\t\t\tlet endIndex = value.end;\n\n\t\t\t\t\twhile (\n\t\t\t\t\t\tvalue.formats[ startIndex ]?.find(\n\t\t\t\t\t\t\t( format ) => format?.type === startFormat.type\n\t\t\t\t\t\t)\n\t\t\t\t\t) {\n\t\t\t\t\t\tstartIndex--;\n\t\t\t\t\t}\n\n\t\t\t\t\tendIndex++;\n\n\t\t\t\t\twhile (\n\t\t\t\t\t\tvalue.formats[ endIndex ]?.find(\n\t\t\t\t\t\t\t( format ) => format?.type === startFormat.type\n\t\t\t\t\t\t)\n\t\t\t\t\t) {\n\t\t\t\t\t\tendIndex++;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...value,\n\t\t\t\t\t\tstart: startIndex + 1,\n\t\t\t\t\t\tend: endIndex,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn value;\n\t\t\t}\n\n\t\t\tonRemoveFormat() {\n\t\t\t\tconst { onChange, speak, value } = this.props;\n\t\t\t\tconst startFormat = getActiveFormat( value, 'core/link' );\n\n\t\t\t\t// Before we try to remove anything we check if there is something at the caret position to remove.\n\t\t\t\tif ( isCollapsed( value ) && startFormat === undefined ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tconst linkSelection = this.getLinkSelection();\n\n\t\t\t\tonChange( removeFormat( linkSelection, name ) );\n\t\t\t\tspeak( __( 'Link removed.' ), 'assertive' );\n\t\t\t}\n\n\t\t\tasync getURLFromClipboard() {\n\t\t\t\tconst clipboardText = await Clipboard.getString();\n\t\t\t\tif ( ! clipboardText ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\t// Check if pasted text is URL.\n\t\t\t\tif ( ! isURL( clipboardText ) ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tthis.setState( { clipboardURL: clipboardText } );\n\t\t\t}\n\n\t\t\trender() {\n\t\t\t\tconst { isActive, activeAttributes, onChange } = this.props;\n\t\t\t\tconst linkSelection = this.getLinkSelection();\n\t\t\t\t// If no URL is set and we have a clipboard URL let's use it.\n\t\t\t\tif ( ! activeAttributes.url && this.state.clipboardURL ) {\n\t\t\t\t\tactiveAttributes.url = this.state.clipboardURL;\n\t\t\t\t}\n\t\t\t\treturn (\n\t\t\t\t\t<>\n\t\t\t\t\t\t<ModalLinkUI\n\t\t\t\t\t\t\tisVisible={ this.state.addingLink }\n\t\t\t\t\t\t\tisActive={ isActive }\n\t\t\t\t\t\t\tactiveAttributes={ activeAttributes }\n\t\t\t\t\t\t\tonClose={ this.stopAddingLink }\n\t\t\t\t\t\t\tonChange={ onChange }\n\t\t\t\t\t\t\tonRemove={ this.onRemoveFormat }\n\t\t\t\t\t\t\tvalue={ linkSelection }\n\t\t\t\t\t\t/>\n\t\t\t\t\t\t<RichTextToolbarButton\n\t\t\t\t\t\t\tname=\"link\"\n\t\t\t\t\t\t\ticon={ linkIcon }\n\t\t\t\t\t\t\ttitle={ __( 'Link' ) }\n\t\t\t\t\t\t\tonClick={ this.addLink }\n\t\t\t\t\t\t\tisActive={ isActive }\n\t\t\t\t\t\t\tsh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AAE/C,KAAK,CAAE+C,aAAc,CAAC,EAAG;QAC/B;MACD;MACA,IAAI,CAACb,QAAQ,CAAE;QAAEC,YAAY,EAAEY;MAAc,CAAE,CAAC;IACjD;IAEAE,MAAMA,CAAA,EAAG;MACR,MAAM;QAAEX,QAAQ;QAAEY,gBAAgB;QAAEzB;MAAS,CAAC,GAAG,IAAI,CAACC,KAAK;MAC3D,MAAMoB,aAAa,GAAG,IAAI,CAACT,gBAAgB,CAAC,CAAC;MAC7C;MACA,IAAK,CAAEa,gBAAgB,CAACxC,GAAG,IAAI,IAAI,CAACY,KAAK,CAACa,YAAY,EAAG;QACxDe,gBAAgB,CAACxC,GAAG,GAAG,IAAI,CAACY,KAAK,CAACa,YAAY;MAC/C;MACA,OACCgB,aAAA,CAAAC,QAAA,QACCD,aAAA,CAAChD,WAAW;QACXkD,SAAS,EAAG,IAAI,CAAC/B,KAAK,CAACC,UAAY;QACnCe,QAAQ,EAAGA,QAAU;QACrBY,gBAAgB,EAAGA,gBAAkB;QACrCI,OAAO,EAAG,IAAI,CAACnC,cAAgB;QAC/BM,QAAQ,EAAGA,QAAU;QACrB8B,QAAQ,EAAG,IAAI,CAACnC,cAAgB;QAChCI,KAAK,EAAGsB;MAAe,CACvB,CAAC,EACFK,aAAA,CAAC1D,qBAAqB;QACrBY,IAAI,EAAC,MAAM;QACXmD,IAAI,EAAGtD,QAAU;QACjBI,KAAK,EAAGhB,EAAE,CAAE,MAAO,CAAG;QACtBmE,OAAO,EAAG,IAAI,CAACxC,OAAS;QACxBqB,QAAQ,EAAGA,QAAU;QACrBoB,YAAY,EAAC,SAAS;QACtBC,iBAAiB,EAAC;MAAG,CACrB,CACA,CAAC;IAEL;EACD,CACD;AACD,CAAC"}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { createElement } from "@wordpress/element";
|
|
2
|
-
|
|
3
2
|
/**
|
|
4
3
|
* WordPress dependencies
|
|
5
4
|
*/
|
|
6
|
-
import {
|
|
5
|
+
import { useRef, createInterpolateElement } from '@wordpress/element';
|
|
7
6
|
import { __, sprintf } from '@wordpress/i18n';
|
|
8
7
|
import { speak } from '@wordpress/a11y';
|
|
9
8
|
import { Popover } from '@wordpress/components';
|
|
@@ -11,14 +10,13 @@ import { prependHTTP } from '@wordpress/url';
|
|
|
11
10
|
import { create, insert, isCollapsed, applyFormat, useAnchor, removeFormat, slice, replace, split, concat } from '@wordpress/rich-text';
|
|
12
11
|
import { __experimentalLinkControl as LinkControl, store as blockEditorStore } from '@wordpress/block-editor';
|
|
13
12
|
import { useSelect } from '@wordpress/data';
|
|
13
|
+
|
|
14
14
|
/**
|
|
15
15
|
* Internal dependencies
|
|
16
16
|
*/
|
|
17
|
-
|
|
18
17
|
import { createLinkFormat, isValidHref, getFormatBoundary } from './utils';
|
|
19
18
|
import { link as settings } from './index';
|
|
20
19
|
import useLinkInstanceKey from './use-link-instance-key';
|
|
21
|
-
|
|
22
20
|
function InlineLinkUI({
|
|
23
21
|
isActive,
|
|
24
22
|
activeAttributes,
|
|
@@ -28,19 +26,10 @@ function InlineLinkUI({
|
|
|
28
26
|
stopAddingLink,
|
|
29
27
|
contentRef
|
|
30
28
|
}) {
|
|
31
|
-
const richLinkTextValue = getRichTextValueFromSelection(value, isActive);
|
|
29
|
+
const richLinkTextValue = getRichTextValueFromSelection(value, isActive);
|
|
32
30
|
|
|
31
|
+
// Get the text content minus any HTML tags.
|
|
33
32
|
const richTextText = richLinkTextValue.text;
|
|
34
|
-
/**
|
|
35
|
-
* Pending settings to be applied to the next link. When inserting a new
|
|
36
|
-
* link, toggle values cannot be applied immediately, because there is not
|
|
37
|
-
* yet a link for them to apply to. Thus, they are maintained in a state
|
|
38
|
-
* value until the time that the link can be inserted or edited.
|
|
39
|
-
*
|
|
40
|
-
* @type {[Object|undefined,Function]}
|
|
41
|
-
*/
|
|
42
|
-
|
|
43
|
-
const [nextLinkValue, setNextLinkValue] = useState();
|
|
44
33
|
const {
|
|
45
34
|
createPageEntity,
|
|
46
35
|
userCanCreatePages
|
|
@@ -48,9 +37,7 @@ function InlineLinkUI({
|
|
|
48
37
|
const {
|
|
49
38
|
getSettings
|
|
50
39
|
} = select(blockEditorStore);
|
|
51
|
-
|
|
52
40
|
const _settings = getSettings();
|
|
53
|
-
|
|
54
41
|
return {
|
|
55
42
|
createPageEntity: _settings.__experimentalCreatePageEntity,
|
|
56
43
|
userCanCreatePages: _settings.__experimentalUserCanCreatePages
|
|
@@ -61,38 +48,25 @@ function InlineLinkUI({
|
|
|
61
48
|
type: activeAttributes.type,
|
|
62
49
|
id: activeAttributes.id,
|
|
63
50
|
opensInNewTab: activeAttributes.target === '_blank',
|
|
64
|
-
title: richTextText
|
|
65
|
-
...nextLinkValue
|
|
51
|
+
title: richTextText
|
|
66
52
|
};
|
|
67
|
-
|
|
68
53
|
function removeLink() {
|
|
69
54
|
const newValue = removeFormat(value, 'core/link');
|
|
70
55
|
onChange(newValue);
|
|
71
56
|
stopAddingLink();
|
|
72
57
|
speak(__('Link removed.'), 'assertive');
|
|
73
58
|
}
|
|
74
|
-
|
|
75
59
|
function onChangeLink(nextValue) {
|
|
76
|
-
//
|
|
77
|
-
//
|
|
78
|
-
// the
|
|
79
|
-
|
|
60
|
+
// LinkControl calls `onChange` immediately upon the toggling a setting.
|
|
61
|
+
// Before merging the next value with the current link value, check if
|
|
62
|
+
// the setting was toggled.
|
|
63
|
+
const didToggleSetting = linkValue.opensInNewTab !== nextValue.opensInNewTab && nextValue.url === undefined;
|
|
64
|
+
|
|
65
|
+
// Merge the next value with the current link value.
|
|
66
|
+
nextValue = {
|
|
67
|
+
...linkValue,
|
|
80
68
|
...nextValue
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const didToggleSetting = linkValue.opensInNewTab !== nextValue.opensInNewTab && linkValue.url === nextValue.url; // If change handler was called as a result of a settings change during
|
|
84
|
-
// link insertion, it must be held in state until the link is ready to
|
|
85
|
-
// be applied.
|
|
86
|
-
|
|
87
|
-
const didToggleSettingForNewLink = didToggleSetting && nextValue.url === undefined; // If link will be assigned, the state value can be considered flushed.
|
|
88
|
-
// Otherwise, persist the pending changes.
|
|
89
|
-
|
|
90
|
-
setNextLinkValue(didToggleSettingForNewLink ? nextValue : undefined);
|
|
91
|
-
|
|
92
|
-
if (didToggleSettingForNewLink) {
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
|
|
69
|
+
};
|
|
96
70
|
const newUrl = prependHTTP(nextValue.url);
|
|
97
71
|
const linkFormat = createLinkFormat({
|
|
98
72
|
url: newUrl,
|
|
@@ -101,7 +75,6 @@ function InlineLinkUI({
|
|
|
101
75
|
opensInNewWindow: nextValue.opensInNewTab
|
|
102
76
|
});
|
|
103
77
|
const newText = nextValue.title || newUrl;
|
|
104
|
-
|
|
105
78
|
if (isCollapsed(value) && !isActive) {
|
|
106
79
|
// Scenario: we don't have any actively selected text or formats.
|
|
107
80
|
const toInsert = applyFormat(create({
|
|
@@ -111,7 +84,6 @@ function InlineLinkUI({
|
|
|
111
84
|
} else {
|
|
112
85
|
// Scenario: we have any active text selection or an active format.
|
|
113
86
|
let newValue;
|
|
114
|
-
|
|
115
87
|
if (newText === richTextText) {
|
|
116
88
|
// If we're not updating the text then ignore.
|
|
117
89
|
newValue = applyFormat(value, linkFormat);
|
|
@@ -120,19 +92,24 @@ function InlineLinkUI({
|
|
|
120
92
|
// can apply formats to it.
|
|
121
93
|
newValue = create({
|
|
122
94
|
text: newText
|
|
123
|
-
});
|
|
95
|
+
});
|
|
124
96
|
|
|
125
|
-
|
|
97
|
+
// Apply the new Link format to this new text value.
|
|
98
|
+
newValue = applyFormat(newValue, linkFormat, 0, newText.length);
|
|
126
99
|
|
|
100
|
+
// Get the boundaries of the active link format.
|
|
127
101
|
const boundary = getFormatBoundary(value, {
|
|
128
102
|
type: 'core/link'
|
|
129
|
-
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// Split the value at the start of the active link format.
|
|
130
106
|
// Passing "start" as the 3rd parameter is required to ensure
|
|
131
107
|
// the second half of the split value is split at the format's
|
|
132
108
|
// start boundary and avoids relying on the value's "end" property
|
|
133
109
|
// which may not correspond correctly.
|
|
110
|
+
const [valBefore, valAfter] = split(value, boundary.start, boundary.start);
|
|
134
111
|
|
|
135
|
-
|
|
112
|
+
// Update the original (full) RichTextValue replacing the
|
|
136
113
|
// target text with the *new* RichTextValue containing:
|
|
137
114
|
// 1. The new text content.
|
|
138
115
|
// 2. The new link format.
|
|
@@ -144,22 +121,21 @@ function InlineLinkUI({
|
|
|
144
121
|
// Note original formats will be lost when applying this change.
|
|
145
122
|
// That is expected behaviour.
|
|
146
123
|
// See: https://github.com/WordPress/gutenberg/pull/33849#issuecomment-936134179.
|
|
147
|
-
|
|
148
124
|
const newValAfter = replace(valAfter, richTextText, newValue);
|
|
149
125
|
newValue = concat(valBefore, newValAfter);
|
|
150
126
|
}
|
|
151
|
-
|
|
152
127
|
newValue.start = newValue.end;
|
|
128
|
+
|
|
129
|
+
// Hides the Link UI.
|
|
153
130
|
newValue.activeFormats = [];
|
|
154
131
|
onChange(newValue);
|
|
155
|
-
}
|
|
156
|
-
// URL is submitted.
|
|
157
|
-
|
|
132
|
+
}
|
|
158
133
|
|
|
134
|
+
// Focus should only be shifted back to the formatted segment when the
|
|
135
|
+
// URL is submitted.
|
|
159
136
|
if (!didToggleSetting) {
|
|
160
137
|
stopAddingLink();
|
|
161
138
|
}
|
|
162
|
-
|
|
163
139
|
if (!isValidHref(newUrl)) {
|
|
164
140
|
speak(__('Warning: the link has been inserted but may have errors. Please test it.'), 'assertive');
|
|
165
141
|
} else if (isActive) {
|
|
@@ -168,20 +144,20 @@ function InlineLinkUI({
|
|
|
168
144
|
speak(__('Link inserted.'), 'assertive');
|
|
169
145
|
}
|
|
170
146
|
}
|
|
171
|
-
|
|
172
147
|
const popoverAnchor = useAnchor({
|
|
173
148
|
editableContentElement: contentRef.current,
|
|
174
149
|
settings
|
|
175
|
-
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// Generate a string based key that is unique to this anchor reference.
|
|
176
153
|
// This is used to force re-mount the LinkControl component to avoid
|
|
177
154
|
// potential stale state bugs caused by the component not being remounted
|
|
178
155
|
// See https://github.com/WordPress/gutenberg/pull/34742.
|
|
156
|
+
const forceRemountKey = useLinkInstanceKey(popoverAnchor);
|
|
179
157
|
|
|
180
|
-
|
|
158
|
+
// The focusOnMount prop shouldn't evolve during render of a Popover
|
|
181
159
|
// otherwise it causes a render of the content.
|
|
182
|
-
|
|
183
160
|
const focusOnMount = useRef(addingLink ? 'firstElement' : false);
|
|
184
|
-
|
|
185
161
|
async function handleCreate(pageTitle) {
|
|
186
162
|
const page = await createPageEntity({
|
|
187
163
|
title: pageTitle,
|
|
@@ -195,15 +171,12 @@ function InlineLinkUI({
|
|
|
195
171
|
kind: 'post-type'
|
|
196
172
|
};
|
|
197
173
|
}
|
|
198
|
-
|
|
199
174
|
function createButtonText(searchTerm) {
|
|
200
|
-
return createInterpolateElement(sprintf(
|
|
201
|
-
/* translators: %s: search term. */
|
|
175
|
+
return createInterpolateElement(sprintf( /* translators: %s: search term. */
|
|
202
176
|
__('Create page: <mark>%s</mark>'), searchTerm), {
|
|
203
177
|
mark: createElement("mark", null)
|
|
204
178
|
});
|
|
205
179
|
}
|
|
206
|
-
|
|
207
180
|
return createElement(Popover, {
|
|
208
181
|
anchor: popoverAnchor,
|
|
209
182
|
focusOnMount: focusOnMount.current,
|
|
@@ -224,27 +197,27 @@ function InlineLinkUI({
|
|
|
224
197
|
hasTextControl: true
|
|
225
198
|
}));
|
|
226
199
|
}
|
|
227
|
-
|
|
228
200
|
function getRichTextValueFromSelection(value, isActive) {
|
|
229
201
|
// Default to the selection ranges on the RichTextValue object.
|
|
230
202
|
let textStart = value.start;
|
|
231
|
-
let textEnd = value.end;
|
|
203
|
+
let textEnd = value.end;
|
|
204
|
+
|
|
205
|
+
// If the format is currently active then the rich text value
|
|
232
206
|
// should always be taken from the bounds of the active format
|
|
233
207
|
// and not the selected text.
|
|
234
|
-
|
|
235
208
|
if (isActive) {
|
|
236
209
|
const boundary = getFormatBoundary(value, {
|
|
237
210
|
type: 'core/link'
|
|
238
211
|
});
|
|
239
|
-
textStart = boundary.start;
|
|
240
|
-
// We account for that here.
|
|
212
|
+
textStart = boundary.start;
|
|
241
213
|
|
|
214
|
+
// Text *selection* always extends +1 beyond the edge of the format.
|
|
215
|
+
// We account for that here.
|
|
242
216
|
textEnd = boundary.end + 1;
|
|
243
|
-
}
|
|
244
|
-
|
|
217
|
+
}
|
|
245
218
|
|
|
219
|
+
// Get a RichTextValue containing the selected text content.
|
|
246
220
|
return slice(value, textStart, textEnd);
|
|
247
221
|
}
|
|
248
|
-
|
|
249
222
|
export default InlineLinkUI;
|
|
250
223
|
//# sourceMappingURL=inline.js.map
|