@wordpress/format-library 4.16.0 → 4.17.0
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
package/build/link/index.js
CHANGED
|
@@ -1,32 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.link = void 0;
|
|
9
|
-
|
|
10
8
|
var _element = require("@wordpress/element");
|
|
11
|
-
|
|
12
9
|
var _i18n = require("@wordpress/i18n");
|
|
13
|
-
|
|
14
10
|
var _richText = require("@wordpress/rich-text");
|
|
15
|
-
|
|
16
11
|
var _url = require("@wordpress/url");
|
|
17
|
-
|
|
18
12
|
var _blockEditor = require("@wordpress/block-editor");
|
|
19
|
-
|
|
20
13
|
var _htmlEntities = require("@wordpress/html-entities");
|
|
21
|
-
|
|
22
14
|
var _icons = require("@wordpress/icons");
|
|
23
|
-
|
|
24
15
|
var _a11y = require("@wordpress/a11y");
|
|
25
|
-
|
|
26
16
|
var _inline = _interopRequireDefault(require("./inline"));
|
|
27
|
-
|
|
28
17
|
var _utils = require("./utils");
|
|
29
|
-
|
|
30
18
|
/**
|
|
31
19
|
* WordPress dependencies
|
|
32
20
|
*/
|
|
@@ -34,9 +22,9 @@ var _utils = require("./utils");
|
|
|
34
22
|
/**
|
|
35
23
|
* Internal dependencies
|
|
36
24
|
*/
|
|
25
|
+
|
|
37
26
|
const name = 'core/link';
|
|
38
27
|
const title = (0, _i18n.__)('Link');
|
|
39
|
-
|
|
40
28
|
function Edit({
|
|
41
29
|
isActive,
|
|
42
30
|
activeAttributes,
|
|
@@ -46,10 +34,8 @@ function Edit({
|
|
|
46
34
|
contentRef
|
|
47
35
|
}) {
|
|
48
36
|
const [addingLink, setAddingLink] = (0, _element.useState)(false);
|
|
49
|
-
|
|
50
37
|
function addLink() {
|
|
51
38
|
const text = (0, _richText.getTextContent)((0, _richText.slice)(value));
|
|
52
|
-
|
|
53
39
|
if (text && (0, _url.isURL)(text) && (0, _utils.isValidHref)(text)) {
|
|
54
40
|
onChange((0, _richText.applyFormat)(value, {
|
|
55
41
|
type: name,
|
|
@@ -68,20 +54,16 @@ function Edit({
|
|
|
68
54
|
setAddingLink(true);
|
|
69
55
|
}
|
|
70
56
|
}
|
|
71
|
-
|
|
72
57
|
function stopAddingLink(returnFocus = true) {
|
|
73
58
|
setAddingLink(false);
|
|
74
|
-
|
|
75
59
|
if (returnFocus) {
|
|
76
60
|
onFocus();
|
|
77
61
|
}
|
|
78
62
|
}
|
|
79
|
-
|
|
80
63
|
function onRemoveFormat() {
|
|
81
64
|
onChange((0, _richText.removeFormat)(value, name));
|
|
82
65
|
(0, _a11y.speak)((0, _i18n.__)('Link removed.'), 'assertive');
|
|
83
66
|
}
|
|
84
|
-
|
|
85
67
|
return (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(_blockEditor.RichTextShortcut, {
|
|
86
68
|
type: "primary",
|
|
87
69
|
character: "k",
|
|
@@ -116,7 +98,6 @@ function Edit({
|
|
|
116
98
|
contentRef: contentRef
|
|
117
99
|
}));
|
|
118
100
|
}
|
|
119
|
-
|
|
120
101
|
const link = {
|
|
121
102
|
name,
|
|
122
103
|
title,
|
|
@@ -128,7 +109,6 @@ const link = {
|
|
|
128
109
|
id: 'data-id',
|
|
129
110
|
target: 'target'
|
|
130
111
|
},
|
|
131
|
-
|
|
132
112
|
__unstablePasteRule(value, {
|
|
133
113
|
html,
|
|
134
114
|
plainText
|
|
@@ -136,14 +116,14 @@ const link = {
|
|
|
136
116
|
if ((0, _richText.isCollapsed)(value)) {
|
|
137
117
|
return value;
|
|
138
118
|
}
|
|
119
|
+
const pastedText = (html || plainText).replace(/<[^>]+>/g, '').trim();
|
|
139
120
|
|
|
140
|
-
|
|
141
|
-
|
|
121
|
+
// A URL was pasted, turn the selection into a link.
|
|
142
122
|
if (!(0, _url.isURL)(pastedText)) {
|
|
143
123
|
return value;
|
|
144
|
-
}
|
|
145
|
-
|
|
124
|
+
}
|
|
146
125
|
|
|
126
|
+
// Allows us to ask for this information when we get a report.
|
|
147
127
|
window.console.log('Created link:\n\n', pastedText);
|
|
148
128
|
return (0, _richText.applyFormat)(value, {
|
|
149
129
|
type: name,
|
|
@@ -152,7 +132,6 @@ const link = {
|
|
|
152
132
|
}
|
|
153
133
|
});
|
|
154
134
|
},
|
|
155
|
-
|
|
156
135
|
edit: Edit
|
|
157
136
|
};
|
|
158
137
|
exports.link = link;
|
package/build/link/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"names":["_element","require","_i18n","_richText","_url","_blockEditor","_htmlEntities","_icons","_a11y","_inline","_interopRequireDefault","_utils","name","title","__","Edit","isActive","activeAttributes","value","onChange","onFocus","contentRef","addingLink","setAddingLink","useState","addLink","text","getTextContent","slice","isURL","isValidHref","applyFormat","type","attributes","url","isEmail","stopAddingLink","returnFocus","onRemoveFormat","removeFormat","speak","createElement","Fragment","RichTextShortcut","character","onUse","RichTextToolbarButton","icon","linkOff","onClick","shortcutType","shortcutCharacter","linkIcon","default","link","tagName","className","id","target","__unstablePasteRule","html","plainText","isCollapsed","pastedText","replace","trim","window","console","log","decodeEntities","edit","exports"],"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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AgD,MAAM,CAACC,OAAO,CAACC,GAAG,CAAE,mBAAmB,EAAEL,UAAW,CAAC;IAErD,OAAO,IAAAhC,qBAAW,EAAEb,KAAK,EAAE;MAC1Bc,IAAI,EAAEpB,IAAI;MACVqB,UAAU,EAAE;QACXC,GAAG,EAAE,IAAAmC,4BAAc,EAAEN,UAAW;MACjC;IACD,CAAE,CAAC;EACJ,CAAC;EACDO,IAAI,EAAEvD;AACP,CAAC;AAACwD,OAAA,CAAAjB,IAAA,GAAAA,IAAA"}
|
|
@@ -1,32 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.link = void 0;
|
|
9
|
-
|
|
10
8
|
var _element = require("@wordpress/element");
|
|
11
|
-
|
|
12
9
|
var _clipboard = _interopRequireDefault(require("@react-native-clipboard/clipboard"));
|
|
13
|
-
|
|
14
10
|
var _i18n = require("@wordpress/i18n");
|
|
15
|
-
|
|
16
11
|
var _components = require("@wordpress/components");
|
|
17
|
-
|
|
18
12
|
var _blockEditor = require("@wordpress/block-editor");
|
|
19
|
-
|
|
20
13
|
var _richText = require("@wordpress/rich-text");
|
|
21
|
-
|
|
22
14
|
var _url = require("@wordpress/url");
|
|
23
|
-
|
|
24
15
|
var _icons = require("@wordpress/icons");
|
|
25
|
-
|
|
26
16
|
var _modal = _interopRequireDefault(require("./modal"));
|
|
27
|
-
|
|
28
17
|
var _utils = require("./utils");
|
|
29
|
-
|
|
30
18
|
/**
|
|
31
19
|
* External dependencies
|
|
32
20
|
*/
|
|
@@ -38,6 +26,7 @@ var _utils = require("./utils");
|
|
|
38
26
|
/**
|
|
39
27
|
* Internal dependencies
|
|
40
28
|
*/
|
|
29
|
+
|
|
41
30
|
const name = 'core/link';
|
|
42
31
|
const link = {
|
|
43
32
|
name,
|
|
@@ -60,14 +49,12 @@ const link = {
|
|
|
60
49
|
addingLink: false
|
|
61
50
|
};
|
|
62
51
|
}
|
|
63
|
-
|
|
64
52
|
addLink() {
|
|
65
53
|
const {
|
|
66
54
|
value,
|
|
67
55
|
onChange
|
|
68
56
|
} = this.props;
|
|
69
57
|
const text = (0, _richText.getTextContent)((0, _richText.slice)(value));
|
|
70
|
-
|
|
71
58
|
if (text && (0, _url.isURL)(text) && (0, _utils.isValidHref)(text)) {
|
|
72
59
|
const newValue = (0, _richText.applyFormat)(value, {
|
|
73
60
|
type: name,
|
|
@@ -77,7 +64,8 @@ const link = {
|
|
|
77
64
|
});
|
|
78
65
|
newValue.start = newValue.end;
|
|
79
66
|
newValue.activeFormats = [];
|
|
80
|
-
onChange({
|
|
67
|
+
onChange({
|
|
68
|
+
...newValue,
|
|
81
69
|
needsSelectionUpdate: true
|
|
82
70
|
});
|
|
83
71
|
} else {
|
|
@@ -87,91 +75,79 @@ const link = {
|
|
|
87
75
|
this.getURLFromClipboard();
|
|
88
76
|
}
|
|
89
77
|
}
|
|
90
|
-
|
|
91
78
|
stopAddingLink() {
|
|
92
79
|
this.setState({
|
|
93
80
|
addingLink: false,
|
|
94
81
|
clipboardURL: undefined
|
|
95
82
|
});
|
|
96
83
|
}
|
|
97
|
-
|
|
98
84
|
getLinkSelection() {
|
|
99
85
|
const {
|
|
100
86
|
value,
|
|
101
87
|
isActive
|
|
102
88
|
} = this.props;
|
|
103
|
-
const startFormat = (0, _richText.getActiveFormat)(value, 'core/link');
|
|
104
|
-
// TODO: handle partly selected links.
|
|
89
|
+
const startFormat = (0, _richText.getActiveFormat)(value, 'core/link');
|
|
105
90
|
|
|
91
|
+
// If the link isn't selected, get the link manually by looking around the cursor
|
|
92
|
+
// TODO: handle partly selected links.
|
|
106
93
|
if (startFormat && (0, _richText.isCollapsed)(value) && isActive) {
|
|
107
94
|
let startIndex = value.start;
|
|
108
95
|
let endIndex = value.end;
|
|
109
|
-
|
|
110
96
|
while (value.formats[startIndex]?.find(format => format?.type === startFormat.type)) {
|
|
111
97
|
startIndex--;
|
|
112
98
|
}
|
|
113
|
-
|
|
114
99
|
endIndex++;
|
|
115
|
-
|
|
116
100
|
while (value.formats[endIndex]?.find(format => format?.type === startFormat.type)) {
|
|
117
101
|
endIndex++;
|
|
118
102
|
}
|
|
119
|
-
|
|
120
|
-
|
|
103
|
+
return {
|
|
104
|
+
...value,
|
|
121
105
|
start: startIndex + 1,
|
|
122
106
|
end: endIndex
|
|
123
107
|
};
|
|
124
108
|
}
|
|
125
|
-
|
|
126
109
|
return value;
|
|
127
110
|
}
|
|
128
|
-
|
|
129
111
|
onRemoveFormat() {
|
|
130
112
|
const {
|
|
131
113
|
onChange,
|
|
132
114
|
speak,
|
|
133
115
|
value
|
|
134
116
|
} = this.props;
|
|
135
|
-
const startFormat = (0, _richText.getActiveFormat)(value, 'core/link');
|
|
117
|
+
const startFormat = (0, _richText.getActiveFormat)(value, 'core/link');
|
|
136
118
|
|
|
119
|
+
// Before we try to remove anything we check if there is something at the caret position to remove.
|
|
137
120
|
if ((0, _richText.isCollapsed)(value) && startFormat === undefined) {
|
|
138
121
|
return;
|
|
139
122
|
}
|
|
140
|
-
|
|
141
123
|
const linkSelection = this.getLinkSelection();
|
|
142
124
|
onChange((0, _richText.removeFormat)(linkSelection, name));
|
|
143
125
|
speak((0, _i18n.__)('Link removed.'), 'assertive');
|
|
144
126
|
}
|
|
145
|
-
|
|
146
127
|
async getURLFromClipboard() {
|
|
147
128
|
const clipboardText = await _clipboard.default.getString();
|
|
148
|
-
|
|
149
129
|
if (!clipboardText) {
|
|
150
130
|
return;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
|
|
131
|
+
}
|
|
132
|
+
// Check if pasted text is URL.
|
|
154
133
|
if (!(0, _url.isURL)(clipboardText)) {
|
|
155
134
|
return;
|
|
156
135
|
}
|
|
157
|
-
|
|
158
136
|
this.setState({
|
|
159
137
|
clipboardURL: clipboardText
|
|
160
138
|
});
|
|
161
139
|
}
|
|
162
|
-
|
|
163
140
|
render() {
|
|
164
141
|
const {
|
|
165
142
|
isActive,
|
|
166
143
|
activeAttributes,
|
|
167
144
|
onChange
|
|
168
145
|
} = this.props;
|
|
169
|
-
const linkSelection = this.getLinkSelection();
|
|
170
|
-
|
|
146
|
+
const linkSelection = this.getLinkSelection();
|
|
147
|
+
// If no URL is set and we have a clipboard URL let's use it.
|
|
171
148
|
if (!activeAttributes.url && this.state.clipboardURL) {
|
|
172
149
|
activeAttributes.url = this.state.clipboardURL;
|
|
173
150
|
}
|
|
174
|
-
|
|
175
151
|
return (0, _element.createElement)(_element.Fragment, null, (0, _element.createElement)(_modal.default, {
|
|
176
152
|
isVisible: this.state.addingLink,
|
|
177
153
|
isActive: isActive,
|
|
@@ -190,7 +166,6 @@ const link = {
|
|
|
190
166
|
shortcutCharacter: "k"
|
|
191
167
|
}));
|
|
192
168
|
}
|
|
193
|
-
|
|
194
169
|
})
|
|
195
170
|
};
|
|
196
171
|
exports.link = link;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/format-library/src/link/index.native.js"],"names":["name","link","title","tagName","className","attributes","url","target","rel","edit","LinkEdit","Component","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","Clipboard","getString","render","activeAttributes","linkIcon"],"mappings":";;;;;;;;;AASA;;AANA;;AAKA;;AAEA;;AACA;;AACA;;AAQA;;AACA;;AAKA;;AACA;;AA3BA;AACA;AACA;;AAGA;AACA;AACA;;AAgBA;AACA;AACA;AAIA,MAAMA,IAAI,GAAG,WAAb;AAEO,MAAMC,IAAI,GAAG;AACnBD,EAAAA,IADmB;AAEnBE,EAAAA,KAAK,EAAE,cAAI,MAAJ,CAFY;AAGnBC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AAA,QAAQ,EAAG,KAAKL,cANjB;AAOC,QAAA,KAAK,EAAG0B;AAPT,QADD,EAUC,4BAAC,kCAAD;AACC,QAAA,IAAI,EAAC,MADN;AAEC,QAAA,IAAI,EAAGM,WAFR;AAGC,QAAA,KAAK,EAAG,cAAI,MAAJ,CAHT;AAIC,QAAA,OAAO,EAAG,KAAKnC,OAJhB;AAKC,QAAA,QAAQ,EAAGqB,QALZ;AAMC,QAAA,YAAY,EAAC,SANd;AAOC,QAAA,iBAAiB,EAAC;AAPnB,QAVD,CADD;AAsBA;;AAlI+B,GAD5B;AAVa,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":["_element","require","_clipboard","_interopRequireDefault","_i18n","_components","_blockEditor","_richText","_url","_icons","_modal","_utils","name","link","title","__","tagName","className","attributes","url","target","rel","edit","withSpokenMessages","LinkEdit","Component","constructor","arguments","addLink","bind","stopAddingLink","onRemoveFormat","getURLFromClipboard","state","addingLink","value","onChange","props","text","getTextContent","slice","isURL","isValidHref","newValue","applyFormat","type","start","end","activeFormats","needsSelectionUpdate","setState","clipboardURL","undefined","getLinkSelection","isActive","startFormat","getActiveFormat","isCollapsed","startIndex","endIndex","formats","find","format","speak","linkSelection","removeFormat","clipboardText","Clipboard","getString","render","activeAttributes","createElement","Fragment","default","isVisible","onClose","onRemove","RichTextToolbarButton","icon","linkIcon","onClick","shortcutType","shortcutCharacter","exports"],"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\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"],"mappings":";;;;;;;AASA,IAAAA,QAAA,GAAAC,OAAA;AANA,IAAAC,UAAA,GAAAC,sBAAA,CAAAF,OAAA;AAKA,IAAAG,KAAA,GAAAH,OAAA;AAEA,IAAAI,WAAA,GAAAJ,OAAA;AACA,IAAAK,YAAA,GAAAL,OAAA;AACA,IAAAM,SAAA,GAAAN,OAAA;AAQA,IAAAO,IAAA,GAAAP,OAAA;AACA,IAAAQ,MAAA,GAAAR,OAAA;AAKA,IAAAS,MAAA,GAAAP,sBAAA,CAAAF,OAAA;AACA,IAAAU,MAAA,GAAAV,OAAA;AA3BA;AACA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AAnD,QAAA,CAAAuE,aAAA,EAAAvE,QAAA,CAAAwE,QAAA,QACC,IAAAxE,QAAA,CAAAuE,aAAA,EAAC7D,MAAA,CAAA+D,OAAW;QACXC,SAAS,EAAG,IAAI,CAACzC,KAAK,CAACC,UAAY;QACnCoB,QAAQ,EAAGA,QAAU;QACrBgB,gBAAgB,EAAGA,gBAAkB;QACrCK,OAAO,EAAG,IAAI,CAAC7C,cAAgB;QAC/BM,QAAQ,EAAGA,QAAU;QACrBwC,QAAQ,EAAG,IAAI,CAAC7C,cAAgB;QAChCI,KAAK,EAAG6B;MAAe,CACvB,CAAC,EACF,IAAAhE,QAAA,CAAAuE,aAAA,EAACjE,YAAA,CAAAuE,qBAAqB;QACrBjE,IAAI,EAAC,MAAM;QACXkE,IAAI,EAAGC,WAAU;QACjBjE,KAAK,EAAG,IAAAC,QAAE,EAAE,MAAO,CAAG;QACtBiE,OAAO,EAAG,IAAI,CAACpD,OAAS;QACxB0B,QAAQ,EAAGA,QAAU;QACrB2B,YAAY,EAAC,SAAS;QACtBC,iBAAiB,EAAC;MAAG,CACrB,CACA,CAAC;IAEL;EACD,CACD;AACD,CAAC;AAACC,OAAA,CAAAtE,IAAA,GAAAA,IAAA"}
|
package/build/link/inline.js
CHANGED
|
@@ -1,34 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = void 0;
|
|
9
|
-
|
|
10
8
|
var _element = require("@wordpress/element");
|
|
11
|
-
|
|
12
9
|
var _i18n = require("@wordpress/i18n");
|
|
13
|
-
|
|
14
10
|
var _a11y = require("@wordpress/a11y");
|
|
15
|
-
|
|
16
11
|
var _components = require("@wordpress/components");
|
|
17
|
-
|
|
18
12
|
var _url = require("@wordpress/url");
|
|
19
|
-
|
|
20
13
|
var _richText = require("@wordpress/rich-text");
|
|
21
|
-
|
|
22
14
|
var _blockEditor = require("@wordpress/block-editor");
|
|
23
|
-
|
|
24
15
|
var _data = require("@wordpress/data");
|
|
25
|
-
|
|
26
16
|
var _utils = require("./utils");
|
|
27
|
-
|
|
28
17
|
var _index = require("./index");
|
|
29
|
-
|
|
30
18
|
var _useLinkInstanceKey = _interopRequireDefault(require("./use-link-instance-key"));
|
|
31
|
-
|
|
32
19
|
/**
|
|
33
20
|
* WordPress dependencies
|
|
34
21
|
*/
|
|
@@ -36,6 +23,7 @@ var _useLinkInstanceKey = _interopRequireDefault(require("./use-link-instance-ke
|
|
|
36
23
|
/**
|
|
37
24
|
* Internal dependencies
|
|
38
25
|
*/
|
|
26
|
+
|
|
39
27
|
function InlineLinkUI({
|
|
40
28
|
isActive,
|
|
41
29
|
activeAttributes,
|
|
@@ -45,19 +33,10 @@ function InlineLinkUI({
|
|
|
45
33
|
stopAddingLink,
|
|
46
34
|
contentRef
|
|
47
35
|
}) {
|
|
48
|
-
const richLinkTextValue = getRichTextValueFromSelection(value, isActive);
|
|
36
|
+
const richLinkTextValue = getRichTextValueFromSelection(value, isActive);
|
|
49
37
|
|
|
38
|
+
// Get the text content minus any HTML tags.
|
|
50
39
|
const richTextText = richLinkTextValue.text;
|
|
51
|
-
/**
|
|
52
|
-
* Pending settings to be applied to the next link. When inserting a new
|
|
53
|
-
* link, toggle values cannot be applied immediately, because there is not
|
|
54
|
-
* yet a link for them to apply to. Thus, they are maintained in a state
|
|
55
|
-
* value until the time that the link can be inserted or edited.
|
|
56
|
-
*
|
|
57
|
-
* @type {[Object|undefined,Function]}
|
|
58
|
-
*/
|
|
59
|
-
|
|
60
|
-
const [nextLinkValue, setNextLinkValue] = (0, _element.useState)();
|
|
61
40
|
const {
|
|
62
41
|
createPageEntity,
|
|
63
42
|
userCanCreatePages
|
|
@@ -65,9 +44,7 @@ function InlineLinkUI({
|
|
|
65
44
|
const {
|
|
66
45
|
getSettings
|
|
67
46
|
} = select(_blockEditor.store);
|
|
68
|
-
|
|
69
47
|
const _settings = getSettings();
|
|
70
|
-
|
|
71
48
|
return {
|
|
72
49
|
createPageEntity: _settings.__experimentalCreatePageEntity,
|
|
73
50
|
userCanCreatePages: _settings.__experimentalUserCanCreatePages
|
|
@@ -78,38 +55,25 @@ function InlineLinkUI({
|
|
|
78
55
|
type: activeAttributes.type,
|
|
79
56
|
id: activeAttributes.id,
|
|
80
57
|
opensInNewTab: activeAttributes.target === '_blank',
|
|
81
|
-
title: richTextText
|
|
82
|
-
...nextLinkValue
|
|
58
|
+
title: richTextText
|
|
83
59
|
};
|
|
84
|
-
|
|
85
60
|
function removeLink() {
|
|
86
61
|
const newValue = (0, _richText.removeFormat)(value, 'core/link');
|
|
87
62
|
onChange(newValue);
|
|
88
63
|
stopAddingLink();
|
|
89
64
|
(0, _a11y.speak)((0, _i18n.__)('Link removed.'), 'assertive');
|
|
90
65
|
}
|
|
91
|
-
|
|
92
66
|
function onChangeLink(nextValue) {
|
|
93
|
-
//
|
|
94
|
-
//
|
|
95
|
-
// the
|
|
96
|
-
|
|
67
|
+
// LinkControl calls `onChange` immediately upon the toggling a setting.
|
|
68
|
+
// Before merging the next value with the current link value, check if
|
|
69
|
+
// the setting was toggled.
|
|
70
|
+
const didToggleSetting = linkValue.opensInNewTab !== nextValue.opensInNewTab && nextValue.url === undefined;
|
|
71
|
+
|
|
72
|
+
// Merge the next value with the current link value.
|
|
73
|
+
nextValue = {
|
|
74
|
+
...linkValue,
|
|
97
75
|
...nextValue
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const didToggleSetting = linkValue.opensInNewTab !== nextValue.opensInNewTab && linkValue.url === nextValue.url; // If change handler was called as a result of a settings change during
|
|
101
|
-
// link insertion, it must be held in state until the link is ready to
|
|
102
|
-
// be applied.
|
|
103
|
-
|
|
104
|
-
const didToggleSettingForNewLink = didToggleSetting && nextValue.url === undefined; // If link will be assigned, the state value can be considered flushed.
|
|
105
|
-
// Otherwise, persist the pending changes.
|
|
106
|
-
|
|
107
|
-
setNextLinkValue(didToggleSettingForNewLink ? nextValue : undefined);
|
|
108
|
-
|
|
109
|
-
if (didToggleSettingForNewLink) {
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
|
|
76
|
+
};
|
|
113
77
|
const newUrl = (0, _url.prependHTTP)(nextValue.url);
|
|
114
78
|
const linkFormat = (0, _utils.createLinkFormat)({
|
|
115
79
|
url: newUrl,
|
|
@@ -118,7 +82,6 @@ function InlineLinkUI({
|
|
|
118
82
|
opensInNewWindow: nextValue.opensInNewTab
|
|
119
83
|
});
|
|
120
84
|
const newText = nextValue.title || newUrl;
|
|
121
|
-
|
|
122
85
|
if ((0, _richText.isCollapsed)(value) && !isActive) {
|
|
123
86
|
// Scenario: we don't have any actively selected text or formats.
|
|
124
87
|
const toInsert = (0, _richText.applyFormat)((0, _richText.create)({
|
|
@@ -128,7 +91,6 @@ function InlineLinkUI({
|
|
|
128
91
|
} else {
|
|
129
92
|
// Scenario: we have any active text selection or an active format.
|
|
130
93
|
let newValue;
|
|
131
|
-
|
|
132
94
|
if (newText === richTextText) {
|
|
133
95
|
// If we're not updating the text then ignore.
|
|
134
96
|
newValue = (0, _richText.applyFormat)(value, linkFormat);
|
|
@@ -137,19 +99,24 @@ function InlineLinkUI({
|
|
|
137
99
|
// can apply formats to it.
|
|
138
100
|
newValue = (0, _richText.create)({
|
|
139
101
|
text: newText
|
|
140
|
-
});
|
|
102
|
+
});
|
|
141
103
|
|
|
142
|
-
|
|
104
|
+
// Apply the new Link format to this new text value.
|
|
105
|
+
newValue = (0, _richText.applyFormat)(newValue, linkFormat, 0, newText.length);
|
|
143
106
|
|
|
107
|
+
// Get the boundaries of the active link format.
|
|
144
108
|
const boundary = (0, _utils.getFormatBoundary)(value, {
|
|
145
109
|
type: 'core/link'
|
|
146
|
-
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Split the value at the start of the active link format.
|
|
147
113
|
// Passing "start" as the 3rd parameter is required to ensure
|
|
148
114
|
// the second half of the split value is split at the format's
|
|
149
115
|
// start boundary and avoids relying on the value's "end" property
|
|
150
116
|
// which may not correspond correctly.
|
|
117
|
+
const [valBefore, valAfter] = (0, _richText.split)(value, boundary.start, boundary.start);
|
|
151
118
|
|
|
152
|
-
|
|
119
|
+
// Update the original (full) RichTextValue replacing the
|
|
153
120
|
// target text with the *new* RichTextValue containing:
|
|
154
121
|
// 1. The new text content.
|
|
155
122
|
// 2. The new link format.
|
|
@@ -161,22 +128,21 @@ function InlineLinkUI({
|
|
|
161
128
|
// Note original formats will be lost when applying this change.
|
|
162
129
|
// That is expected behaviour.
|
|
163
130
|
// See: https://github.com/WordPress/gutenberg/pull/33849#issuecomment-936134179.
|
|
164
|
-
|
|
165
131
|
const newValAfter = (0, _richText.replace)(valAfter, richTextText, newValue);
|
|
166
132
|
newValue = (0, _richText.concat)(valBefore, newValAfter);
|
|
167
133
|
}
|
|
168
|
-
|
|
169
134
|
newValue.start = newValue.end;
|
|
135
|
+
|
|
136
|
+
// Hides the Link UI.
|
|
170
137
|
newValue.activeFormats = [];
|
|
171
138
|
onChange(newValue);
|
|
172
|
-
}
|
|
173
|
-
// URL is submitted.
|
|
174
|
-
|
|
139
|
+
}
|
|
175
140
|
|
|
141
|
+
// Focus should only be shifted back to the formatted segment when the
|
|
142
|
+
// URL is submitted.
|
|
176
143
|
if (!didToggleSetting) {
|
|
177
144
|
stopAddingLink();
|
|
178
145
|
}
|
|
179
|
-
|
|
180
146
|
if (!(0, _utils.isValidHref)(newUrl)) {
|
|
181
147
|
(0, _a11y.speak)((0, _i18n.__)('Warning: the link has been inserted but may have errors. Please test it.'), 'assertive');
|
|
182
148
|
} else if (isActive) {
|
|
@@ -185,20 +151,20 @@ function InlineLinkUI({
|
|
|
185
151
|
(0, _a11y.speak)((0, _i18n.__)('Link inserted.'), 'assertive');
|
|
186
152
|
}
|
|
187
153
|
}
|
|
188
|
-
|
|
189
154
|
const popoverAnchor = (0, _richText.useAnchor)({
|
|
190
155
|
editableContentElement: contentRef.current,
|
|
191
156
|
settings: _index.link
|
|
192
|
-
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// Generate a string based key that is unique to this anchor reference.
|
|
193
160
|
// This is used to force re-mount the LinkControl component to avoid
|
|
194
161
|
// potential stale state bugs caused by the component not being remounted
|
|
195
162
|
// See https://github.com/WordPress/gutenberg/pull/34742.
|
|
163
|
+
const forceRemountKey = (0, _useLinkInstanceKey.default)(popoverAnchor);
|
|
196
164
|
|
|
197
|
-
|
|
165
|
+
// The focusOnMount prop shouldn't evolve during render of a Popover
|
|
198
166
|
// otherwise it causes a render of the content.
|
|
199
|
-
|
|
200
167
|
const focusOnMount = (0, _element.useRef)(addingLink ? 'firstElement' : false);
|
|
201
|
-
|
|
202
168
|
async function handleCreate(pageTitle) {
|
|
203
169
|
const page = await createPageEntity({
|
|
204
170
|
title: pageTitle,
|
|
@@ -212,15 +178,12 @@ function InlineLinkUI({
|
|
|
212
178
|
kind: 'post-type'
|
|
213
179
|
};
|
|
214
180
|
}
|
|
215
|
-
|
|
216
181
|
function createButtonText(searchTerm) {
|
|
217
|
-
return (0, _element.createInterpolateElement)((0, _i18n.sprintf)(
|
|
218
|
-
/* translators: %s: search term. */
|
|
182
|
+
return (0, _element.createInterpolateElement)((0, _i18n.sprintf)( /* translators: %s: search term. */
|
|
219
183
|
(0, _i18n.__)('Create page: <mark>%s</mark>'), searchTerm), {
|
|
220
184
|
mark: (0, _element.createElement)("mark", null)
|
|
221
185
|
});
|
|
222
186
|
}
|
|
223
|
-
|
|
224
187
|
return (0, _element.createElement)(_components.Popover, {
|
|
225
188
|
anchor: popoverAnchor,
|
|
226
189
|
focusOnMount: focusOnMount.current,
|
|
@@ -241,28 +204,28 @@ function InlineLinkUI({
|
|
|
241
204
|
hasTextControl: true
|
|
242
205
|
}));
|
|
243
206
|
}
|
|
244
|
-
|
|
245
207
|
function getRichTextValueFromSelection(value, isActive) {
|
|
246
208
|
// Default to the selection ranges on the RichTextValue object.
|
|
247
209
|
let textStart = value.start;
|
|
248
|
-
let textEnd = value.end;
|
|
210
|
+
let textEnd = value.end;
|
|
211
|
+
|
|
212
|
+
// If the format is currently active then the rich text value
|
|
249
213
|
// should always be taken from the bounds of the active format
|
|
250
214
|
// and not the selected text.
|
|
251
|
-
|
|
252
215
|
if (isActive) {
|
|
253
216
|
const boundary = (0, _utils.getFormatBoundary)(value, {
|
|
254
217
|
type: 'core/link'
|
|
255
218
|
});
|
|
256
|
-
textStart = boundary.start;
|
|
257
|
-
// We account for that here.
|
|
219
|
+
textStart = boundary.start;
|
|
258
220
|
|
|
221
|
+
// Text *selection* always extends +1 beyond the edge of the format.
|
|
222
|
+
// We account for that here.
|
|
259
223
|
textEnd = boundary.end + 1;
|
|
260
|
-
}
|
|
261
|
-
|
|
224
|
+
}
|
|
262
225
|
|
|
226
|
+
// Get a RichTextValue containing the selected text content.
|
|
263
227
|
return (0, _richText.slice)(value, textStart, textEnd);
|
|
264
228
|
}
|
|
265
|
-
|
|
266
229
|
var _default = InlineLinkUI;
|
|
267
230
|
exports.default = _default;
|
|
268
231
|
//# sourceMappingURL=inline.js.map
|