store
2 months ago
GenerateBgImage.js
2 months ago
block-duplicate-helper.js
2 months ago
capitalize.js
2 months ago
color-code-to-class.js
2 months ago
color-slug-to-color-code.js
2 months ago
common.scss
2 weeks ago
convert-to-grid.js
2 months ago
default-color-palette.js
2 months ago
delete-from-array.js
2 months ago
depModules.js
2 months ago
disable-links.js
2 weeks ago
empty-string-to-undefined.js
2 months ago
example-data.js
2 months ago
fixBrokenUnicode.js
2 months ago
font-awesome-new.js
2 weeks ago
font-awesome-version.js
2 months ago
font-awesome.js
2 months ago
formatNumCol.js
2 months ago
gradient-slug-to-gradient-code.js
2 months ago
heading-level-utils.js
2 months ago
hex-to-rgba.js
2 months ago
horizontal-scroll-controls.js
2 months ago
icon-label.js
2 weeks ago
is-excludes-blocks.js
2 months ago
is-gradient-style.js
2 months ago
is-hex-color.js
2 months ago
is-not-json.js
2 months ago
is-parent-reusable-block.js
16 hours ago
merge-colors.js
2 months ago
multi-array-flaten.js
2 months ago
removeProperty.js
2 months ago
replaceClientId.js
2 months ago
sanitizeSlug.js
2 months ago
settings-page-url.js
2 weeks ago
strip-html.js
2 months ago
taxonomy-utils.js
2 months ago
to-number.js
2 months ago
to-preset-spacing-var.js
2 months ago
unit-options.js
2 months ago
wrap-unwrap.js
2 months ago
disable-links.js
69 lines
| 1 | import { useCallback, useEffect } from '@wordpress/element'; |
| 2 | |
| 3 | const DEFAULT_LINK_SELECTOR = |
| 4 | '.vk_post_imgOuter a, .vk_post .vk_post_title a, .postListText_title a, .card-intext .card-intext-inner, .postListText_singleTermLabel_inner, .vk_post_btnOuter a, .vk_post_taxonomy_terms a'; |
| 5 | |
| 6 | const PROCESSED_MARK = 'data-vk-disable-links-processed'; |
| 7 | |
| 8 | const defaultLinkProcessor = (link) => { |
| 9 | if (link.hasAttribute(PROCESSED_MARK)) { |
| 10 | return; |
| 11 | } |
| 12 | |
| 13 | link.addEventListener('click', (event) => { |
| 14 | event.preventDefault(); |
| 15 | }); |
| 16 | link.style.cursor = 'default'; |
| 17 | link.style.boxShadow = 'unset'; |
| 18 | link.style.textDecorationColor = 'inherit'; |
| 19 | link.setAttribute(PROCESSED_MARK, 'true'); |
| 20 | }; |
| 21 | |
| 22 | export const useDisableLinks = ({ |
| 23 | selector = DEFAULT_LINK_SELECTOR, |
| 24 | onLinkProcess = defaultLinkProcessor, |
| 25 | deps = [], |
| 26 | } = {}) => { |
| 27 | const disableLinks = useCallback(() => { |
| 28 | if (typeof document === 'undefined') { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | const iframe = document.querySelector( |
| 33 | '.block-editor-iframe__container iframe' |
| 34 | ); |
| 35 | const targetDocument = iframe?.contentWindow?.document || document; |
| 36 | |
| 37 | const links = targetDocument.querySelectorAll(selector); |
| 38 | links.forEach(onLinkProcess); |
| 39 | }, [selector, onLinkProcess]); |
| 40 | |
| 41 | useEffect(() => { |
| 42 | if (typeof document === 'undefined') { |
| 43 | return undefined; |
| 44 | } |
| 45 | |
| 46 | const iframe = document.querySelector( |
| 47 | '.block-editor-iframe__container iframe' |
| 48 | ); |
| 49 | const targetDocument = iframe?.contentWindow?.document || document; |
| 50 | const observerTarget = targetDocument.querySelector('body'); |
| 51 | |
| 52 | const observer = new MutationObserver(disableLinks); |
| 53 | if (observerTarget) { |
| 54 | observer.observe(observerTarget, { |
| 55 | childList: true, |
| 56 | subtree: true, |
| 57 | }); |
| 58 | } |
| 59 | |
| 60 | disableLinks(); |
| 61 | |
| 62 | return () => observer.disconnect(); |
| 63 | }, [disableLinks, ...deps]); |
| 64 | |
| 65 | return disableLinks; |
| 66 | }; |
| 67 | |
| 68 | export const editorLinkSelector = DEFAULT_LINK_SELECTOR; |
| 69 |