|
1 | 1 | const { domReady } = wp; |
| 2 | +const { createNotice } = wp.data.dispatch('core/notices'); |
2 | 3 | const { PluginSidebar, PluginSidebarMoreMenuItem } = wp.editor; |
3 | 4 | const { TextareaControl, Button, ToggleControl, Icon } = wp.components; |
4 | 5 | const { withSelect, withDispatch, subscribe } = wp.data; |
@@ -302,11 +303,18 @@ const ImportantTermsComponent = compose([ |
302 | 303 | })), |
303 | 304 | withDispatch(dispatch => ({ |
304 | 305 | setMetaFieldValue: value => { |
305 | | - const editor = dispatch('core/editor'); |
306 | | - let content = selectData('core/editor').getEditedPostContent(); |
307 | | - content = removeHighlightingFromContent(content); |
308 | | - editor.editPost({ content: content }); |
309 | | - editor.editPost({ meta: { _important_terms: value } }); |
| 306 | + return new Promise((resolve, reject) => { |
| 307 | + try { |
| 308 | + const editor = dispatch('core/editor'); |
| 309 | + let content = selectData('core/editor').getEditedPostContent(); |
| 310 | + content = removeHighlightingFromContent(content); |
| 311 | + editor.editPost({ content: content }); |
| 312 | + editor.editPost({ meta: { _important_terms: value } }); |
| 313 | + resolve(); |
| 314 | + } catch (error) { |
| 315 | + reject(error); |
| 316 | + } |
| 317 | + }); |
310 | 318 | } |
311 | 319 | })) |
312 | 320 | ])((props) => { |
@@ -396,19 +404,38 @@ const ImportantTermsComponent = compose([ |
396 | 404 | terms = terms.filter(term => !term.includes('==')); |
397 | 405 | terms = [...new Set(terms)]; |
398 | 406 | const cleanedTerms = terms.join('\n'); |
399 | | - props.setMetaFieldValue(cleanedTerms); |
400 | | - setLocalTerms(cleanedTerms); |
401 | 407 |
|
402 | | - // Update processed terms array and rehighlight if enabled |
403 | | - if (globalHighlightingState) { |
404 | | - processedTermsArray = terms.sort((a, b) => b.length - a.length); |
405 | | - removeHighlighting(); |
406 | | - setTimeout(() => { |
407 | | - if (processedTermsArray.length > 0) { |
408 | | - highlightTerms(processedTermsArray); |
| 408 | + props.setMetaFieldValue(cleanedTerms).then(() => { |
| 409 | + setLocalTerms(cleanedTerms); |
| 410 | + |
| 411 | + if (globalHighlightingState) { |
| 412 | + processedTermsArray = terms.sort((a, b) => b.length - a.length); |
| 413 | + removeHighlighting(); |
| 414 | + setTimeout(() => { |
| 415 | + if (processedTermsArray.length > 0) { |
| 416 | + highlightTerms(processedTermsArray); |
| 417 | + } |
| 418 | + }, 50); |
| 419 | + } |
| 420 | + |
| 421 | + createNotice( |
| 422 | + 'success', |
| 423 | + 'Terms saved successfully.', |
| 424 | + { |
| 425 | + type: 'snackbar', |
| 426 | + isDismissible: true, |
409 | 427 | } |
410 | | - }, 50); |
411 | | - } |
| 428 | + ); |
| 429 | + }).catch(() => { |
| 430 | + createNotice( |
| 431 | + 'error', |
| 432 | + 'Failed to save terms. Please try again.', |
| 433 | + { |
| 434 | + type: 'snackbar', |
| 435 | + isDismissible: true, |
| 436 | + } |
| 437 | + ); |
| 438 | + }); |
412 | 439 | }; |
413 | 440 |
|
414 | 441 | return termsHighlighterEl( |
|
0 commit comments