ColorPopup.js
4 days ago
ColorPopup.scss
4 days ago
EntitySearchDropdown.js
4 days ago
EntitySearchDropdown.scss
4 days ago
LoadSelect.js
5 years ago
ProBadge.js
5 years ago
SelectMediaDropdown.js
4 days ago
SelectMediaDropdown.scss
4 days ago
Tag.js
4 days ago
Tag.scss
4 days ago
TranscriptionLanguageSelect.js
4 days ago
UrlSelect.js
4 days ago
UrlSelect.scss
4 days ago
VideoIcon.js
4 days ago
VideoIcon.scss
4 days ago
UrlSelect.js
129 lines
| 1 | const { __ } = wp.i18n; |
| 2 | const { Button, Popover, Icon } = wp.components; |
| 3 | |
| 4 | const { __experimentalLinkControl: LinkControl } = wp.blockEditor; |
| 5 | const { useState } = wp.element; |
| 6 | const { prependHTTP } = wp.url; |
| 7 | |
| 8 | import "./UrlSelect.scss"; |
| 9 | |
| 10 | export default ({ setSettings, settings }) => { |
| 11 | const [visible, setVisible] = useState(false); |
| 12 | const { url } = settings; |
| 13 | |
| 14 | /** |
| 15 | * Pending settings to be applied to the next link. When inserting a new |
| 16 | * link, toggle values cannot be applied immediately, because there is not |
| 17 | * yet a link for them to apply to. Thus, they are maintained in a state |
| 18 | * value until the time that the link can be inserted or edited. |
| 19 | * |
| 20 | * @type {[Object|undefined,Function]} |
| 21 | */ |
| 22 | const [nextLinkValue, setNextLinkValue] = useState(); |
| 23 | |
| 24 | const linkValue = { |
| 25 | url: settings?.url, |
| 26 | type: settings?.type, |
| 27 | id: settings?.id, |
| 28 | opensInNewTab: settings?.opensInNewTab, |
| 29 | ...nextLinkValue, |
| 30 | }; |
| 31 | |
| 32 | const onChangeLink = (nextValue) => { |
| 33 | // Merge with values from state, both for the purpose of assigning the |
| 34 | // next state value, and for use in constructing the new link format if |
| 35 | // the link is ready to be applied. |
| 36 | nextValue = { |
| 37 | ...nextLinkValue, |
| 38 | ...nextValue, |
| 39 | }; |
| 40 | |
| 41 | // LinkControl calls `onChange` immediately upon the toggling a setting. |
| 42 | const didToggleSetting = |
| 43 | linkValue.opensInNewTab !== nextValue.opensInNewTab && |
| 44 | linkValue.url === nextValue.url; |
| 45 | |
| 46 | // If change handler was called as a result of a settings change during |
| 47 | // link insertion, it must be held in state until the link is ready to |
| 48 | // be applied. |
| 49 | const didToggleSettingForNewLink = |
| 50 | didToggleSetting && nextValue.url === undefined; |
| 51 | |
| 52 | // If link will be assigned, the state value can be considered flushed. |
| 53 | // Otherwise, persist the pending changes. |
| 54 | setNextLinkValue(didToggleSettingForNewLink ? nextValue : undefined); |
| 55 | |
| 56 | if (didToggleSettingForNewLink) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | const newUrl = prependHTTP(nextValue.url); |
| 61 | setSettings({ |
| 62 | url: newUrl, |
| 63 | type: nextValue.type, |
| 64 | id: |
| 65 | nextValue.id !== undefined && nextValue.id !== null |
| 66 | ? String(nextValue.id) |
| 67 | : undefined, |
| 68 | opensInNewTab: nextValue.opensInNewTab, |
| 69 | }); |
| 70 | }; |
| 71 | |
| 72 | const confirmTrash = () => { |
| 73 | const r = confirm( |
| 74 | __("Are you sure you wish to remove this link?", "presto-player") |
| 75 | ); |
| 76 | if (r) { |
| 77 | setSettings({}); |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | return ( |
| 82 | <span> |
| 83 | {url ? ( |
| 84 | <div className="presto-url-select__display"> |
| 85 | <div className="presto-url-select__url-container"> |
| 86 | <a |
| 87 | href="#" |
| 88 | className="presto-url-select__link" |
| 89 | onClick={() => setVisible(!visible)} |
| 90 | > |
| 91 | <Icon |
| 92 | icon="edit" |
| 93 | className="presto-url-select__edit-icon" |
| 94 | /> |
| 95 | {url} |
| 96 | </a> |
| 97 | {visible && ( |
| 98 | <Popover |
| 99 | position="bottom center" |
| 100 | onClose={() => setVisible(false)} |
| 101 | > |
| 102 | <LinkControl value={settings} onChange={onChangeLink} /> |
| 103 | </Popover> |
| 104 | )} |
| 105 | </div> |
| 106 | <div className="presto-url-select__actions"> |
| 107 | <Icon |
| 108 | onClick={confirmTrash} |
| 109 | icon="trash" |
| 110 | className="presto-url-select__trash-icon" |
| 111 | /> |
| 112 | </div> |
| 113 | </div> |
| 114 | ) : ( |
| 115 | <span> |
| 116 | <Button isPrimary isSmall onClick={() => setVisible(!visible)}> |
| 117 | {__("Add Link", "presto-player")} |
| 118 | </Button> |
| 119 | {visible && ( |
| 120 | <Popover className="presto-url-select__popover" position="bottom right" onClose={() => setVisible(false)}> |
| 121 | <LinkControl value={settings} onChange={onChangeLink} /> |
| 122 | </Popover> |
| 123 | )} |
| 124 | </span> |
| 125 | )} |
| 126 | </span> |
| 127 | ); |
| 128 | }; |
| 129 |