ColorPopup.js
2 years ago
LoadSelect.js
5 years ago
ProBadge.js
5 years ago
UrlSelect.js
2 years ago
UrlSelect.js
178 lines
| 1 | /** @jsx jsx */ |
| 2 | const { __ } = wp.i18n; |
| 3 | const { Button, Popover, Icon } = wp.components; |
| 4 | |
| 5 | const { __experimentalLinkControl: LinkControl } = wp.blockEditor; |
| 6 | const { useState } = wp.element; |
| 7 | const { prependHTTP } = wp.url; |
| 8 | |
| 9 | import { css, jsx } from "@emotion/core"; |
| 10 | |
| 11 | export default ({ setSettings, settings }) => { |
| 12 | const [visible, setVisible] = useState(false); |
| 13 | const { url } = settings; |
| 14 | |
| 15 | /** |
| 16 | * Pending settings to be applied to the next link. When inserting a new |
| 17 | * link, toggle values cannot be applied immediately, because there is not |
| 18 | * yet a link for them to apply to. Thus, they are maintained in a state |
| 19 | * value until the time that the link can be inserted or edited. |
| 20 | * |
| 21 | * @type {[Object|undefined,Function]} |
| 22 | */ |
| 23 | const [nextLinkValue, setNextLinkValue] = useState(); |
| 24 | |
| 25 | const linkValue = { |
| 26 | url: settings?.url, |
| 27 | type: settings?.type, |
| 28 | id: settings?.id, |
| 29 | opensInNewTab: settings?.opensInNewTab, |
| 30 | ...nextLinkValue, |
| 31 | }; |
| 32 | |
| 33 | const onChangeLink = (nextValue) => { |
| 34 | // Merge with values from state, both for the purpose of assigning the |
| 35 | // next state value, and for use in constructing the new link format if |
| 36 | // the link is ready to be applied. |
| 37 | nextValue = { |
| 38 | ...nextLinkValue, |
| 39 | ...nextValue, |
| 40 | }; |
| 41 | |
| 42 | // LinkControl calls `onChange` immediately upon the toggling a setting. |
| 43 | const didToggleSetting = |
| 44 | linkValue.opensInNewTab !== nextValue.opensInNewTab && |
| 45 | linkValue.url === nextValue.url; |
| 46 | |
| 47 | // If change handler was called as a result of a settings change during |
| 48 | // link insertion, it must be held in state until the link is ready to |
| 49 | // be applied. |
| 50 | const didToggleSettingForNewLink = |
| 51 | didToggleSetting && nextValue.url === undefined; |
| 52 | |
| 53 | // If link will be assigned, the state value can be considered flushed. |
| 54 | // Otherwise, persist the pending changes. |
| 55 | setNextLinkValue(didToggleSettingForNewLink ? nextValue : undefined); |
| 56 | |
| 57 | if (didToggleSettingForNewLink) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | const newUrl = prependHTTP(nextValue.url); |
| 62 | setSettings({ |
| 63 | url: newUrl, |
| 64 | type: nextValue.type, |
| 65 | id: |
| 66 | nextValue.id !== undefined && nextValue.id !== null |
| 67 | ? String(nextValue.id) |
| 68 | : undefined, |
| 69 | opensInNewTab: nextValue.opensInNewTab, |
| 70 | }); |
| 71 | }; |
| 72 | |
| 73 | const confirmTrash = () => { |
| 74 | const r = confirm( |
| 75 | __("Are you sure you wish to remove this link?", "presto-player") |
| 76 | ); |
| 77 | if (r) { |
| 78 | setSettings({}); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | return ( |
| 83 | <span> |
| 84 | {url ? ( |
| 85 | <div |
| 86 | css={css` |
| 87 | display: flex; |
| 88 | justify-content: space-between; |
| 89 | `} |
| 90 | > |
| 91 | <div |
| 92 | css={css` |
| 93 | max-width: 85%; |
| 94 | overflow: hidden; |
| 95 | display: flex; |
| 96 | align-items: center; |
| 97 | `} |
| 98 | > |
| 99 | <a |
| 100 | href="#" |
| 101 | css={css` |
| 102 | padding: 10px; |
| 103 | background: #f3f3f3; |
| 104 | border-radius: 4px; |
| 105 | width: 100%; |
| 106 | display: inline-flex; |
| 107 | align-items: center; |
| 108 | white-space: nowrap; |
| 109 | overflow: hidden; |
| 110 | text-overflow: ellipsis; |
| 111 | text-decoration: none; |
| 112 | `} |
| 113 | onClick={() => setVisible(!visible)} |
| 114 | > |
| 115 | <Icon |
| 116 | icon="edit" |
| 117 | css={css` |
| 118 | cursor: pointer; |
| 119 | opacity: 0.75; |
| 120 | margin: 0 2px; |
| 121 | font-size: 16px; |
| 122 | width: 16px; |
| 123 | height: 16px; |
| 124 | text-decoration: none; |
| 125 | `} |
| 126 | /> |
| 127 | {url} |
| 128 | </a> |
| 129 | {visible && ( |
| 130 | <Popover |
| 131 | position="bottom center" |
| 132 | onClose={() => setVisible(false)} |
| 133 | > |
| 134 | <LinkControl value={settings} onChange={onChangeLink} /> |
| 135 | </Popover> |
| 136 | )} |
| 137 | </div> |
| 138 | <div |
| 139 | css={css` |
| 140 | display: flex; |
| 141 | align-items: center; |
| 142 | `} |
| 143 | > |
| 144 | <Icon |
| 145 | onClick={confirmTrash} |
| 146 | icon="trash" |
| 147 | className="presto-icon" |
| 148 | css={css` |
| 149 | cursor: pointer; |
| 150 | opacity: 0.75; |
| 151 | margin: 0 2px; |
| 152 | fontsize: 18px; |
| 153 | width: 18px; |
| 154 | height: 18px; |
| 155 | |
| 156 | &:hover { |
| 157 | color: #cc1818; |
| 158 | } |
| 159 | `} |
| 160 | /> |
| 161 | </div> |
| 162 | </div> |
| 163 | ) : ( |
| 164 | <span> |
| 165 | <Button isPrimary isSmall onClick={() => setVisible(!visible)}> |
| 166 | {__("Add Link", "presto-player")} |
| 167 | </Button> |
| 168 | {visible && ( |
| 169 | <Popover css={css`margin-top: 10px`} position="bottom right" onClose={() => setVisible(false)}> |
| 170 | <LinkControl value={settings} onChange={onChangeLink} /> |
| 171 | </Popover> |
| 172 | )} |
| 173 | </span> |
| 174 | )} |
| 175 | </span> |
| 176 | ); |
| 177 | }; |
| 178 |