Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed broken blocks in the latest gutenberg #302

Merged
merged 7 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions assets/components/document-tabs/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,37 @@
margin-bottom: -1px;
background-color: #fff;
z-index: 2;

> ul {
display: flex;

li {
margin-bottom: 0;
}

button {
position: relative;
height: 48px;
font-weight: 500;
outline: none;
box-shadow: none;

&.is-active::after {
height: calc(1 * var(--wp-admin-border-width-focus));
outline: transparent solid 2px;
outline-offset: -1px;
content: "";
position: absolute;
right: 0;
bottom: 0;
left: 0;
pointer-events: none;
background: var(--wp-components-color-accent, var(--wp-admin-theme-color, #3858e9));
border-radius: 0;
transition: all 0.1s linear 0s;
}
}
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions assets/components/preview-server-callback/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default function PreviewServerCallback(props) {
const isMountedRef = useRef(true);
const currentFetchRequest = useRef(null);
const fetchTimeout = useRef();
const blockContentWrapper = useRef();

const prevProps = usePrevious(props);

Expand Down Expand Up @@ -228,12 +229,20 @@ export default function PreviewServerCallback(props) {
result = (
<>
{response ? (
<RenderBlockContent content={response} props={props} />
<RenderBlockContent
content={response}
props={props}
blockContentWrapper={blockContentWrapper}
/>
) : null}
{isLoading ? <Spinner /> : null}
</>
);
}

return <div className="lzb-preview-server">{result}</div>;
return (
<div ref={blockContentWrapper} className="lzb-preview-server">
{result}
</div>
);
}
31 changes: 14 additions & 17 deletions assets/components/preview-server-callback/render-block-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,9 @@ import json5 from 'json5';
// eslint-disable-next-line import/no-extraneous-dependencies
import { isEqual } from 'lodash';
import { __, sprintf } from '@wordpress/i18n';
import { useState, useEffect, useContext } from '@wordpress/element';
import { useState, useEffect } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import {
InnerBlocks,
useInnerBlocksProps,
BlockList,
} from '@wordpress/block-editor';

const { elementContext: __stableElementContext, __unstableElementContext } =
BlockList;
const elementContext = __stableElementContext || __unstableElementContext;
import { InnerBlocks, useInnerBlocksProps } from '@wordpress/block-editor';

const CONVERT_ATTRIBUTES = {
classname: 'className',
Expand All @@ -41,13 +33,10 @@ const CONVERT_TO_JSON = [
];

function RenderScript(props) {
const { src = '', innerHTML = '' } = props;

const element = useContext(elementContext);
const { src = '', innerHTML = '', blockContentWrapper } = props;

useEffect(() => {
const doc = element?.ownerDocument || document;

const doc = blockContentWrapper.current?.ownerDocument || document;
const script = doc.createElement('script');

if (src) {
Expand All @@ -64,7 +53,7 @@ function RenderScript(props) {
// to prevent unwanted JS errors.
doc?.body?.removeChild(script);
};
}, [element, src, innerHTML]);
}, [blockContentWrapper, src, innerHTML]);
}

/**
Expand Down Expand Up @@ -116,7 +105,11 @@ function prepareAttributes(attrs) {
return newAttrs;
}

export default function RenderBlockContent({ content, props }) {
export default function RenderBlockContent({
content,
props,
blockContentWrapper,
}) {
const [innerBlocksOptions, setInnerBlocksOptions] = useState({});

const { hasChildBlocks } = useSelect(
Expand Down Expand Up @@ -207,6 +200,10 @@ export default function RenderBlockContent({ content, props }) {
scriptData.innerHTML = domNode.children[0].data;
}

if (typeof blockContentWrapper !== 'undefined') {
scriptData.blockContentWrapper = blockContentWrapper;
}

return <RenderScript {...scriptData} />;
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { __, sprintf } from '@wordpress/i18n';
import { useEffect, useRef } from '@wordpress/element';
import { select } from '@wordpress/data';

import FixCssFrame from '../fix-css-frame';

// Add autocompleter with control names.
addCompleter({
getCompletions(editor, session, pos, prefix, callback) {
Expand Down Expand Up @@ -143,6 +145,7 @@ export default function CodeEditor(props) {
...(props.setOptions || {}),
}}
/>
<FixCssFrame />
</div>
);
}
15 changes: 6 additions & 9 deletions assets/editor-constructor/boxes/code/fix-css-frame.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/**
* WordPress dependencies
*/
import { useEffect, useContext } from '@wordpress/element';
import { BlockList } from '@wordpress/block-editor';

const { elementContext: __stableElementContext, __unstableElementContext } =
BlockList;
const elementContext = __stableElementContext || __unstableElementContext;
import { useEffect, useRef } from '@wordpress/element';

const STYLE_IDS = [
'autocompletion.css',
Expand All @@ -18,10 +13,12 @@ const STYLE_IDS = [
];

export default function FixCssFrame() {
const element = useContext(elementContext);
const codeWrapper = useRef();

// Find available styles and save it to the state.
useEffect(() => {
const element = codeWrapper.current;

if (!element) {
return;
}
Expand All @@ -48,7 +45,7 @@ export default function FixCssFrame() {

documentFrame.head.appendChild(styleTag.cloneNode(true));
});
}, [element]);
}, [codeWrapper]);

return null;
return <link ref={codeWrapper} />;
}
2 changes: 0 additions & 2 deletions assets/editor-constructor/boxes/code/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
* Internal dependencies.
*/
import CodeEditor from './component-react-ace';
import FixCssFrame from './fix-css-frame';

const { plugin_version: pluginVersion } = window.lazyblocksConstructorData;

Expand Down Expand Up @@ -152,7 +151,6 @@ export default function CustomCodeSettings(props) {
id: `lzb-editor-${data.code_output_method}`,
}}
/>
<FixCssFrame />
</BaseControl>
<BaseControl>
{!showInfo ? (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
/**
* WordPress dependencies.
*/
import { useContext, useEffect } from '@wordpress/element';
import { useEffect, useRef } from '@wordpress/element';
import { select, dispatch } from '@wordpress/data';
import { BlockList } from '@wordpress/block-editor';

const { elementContext: __stableElementContext, __unstableElementContext } =
BlockList;

const elementContext = __stableElementContext || __unstableElementContext;

export default function DeselectActiveControlOnClickOutside() {
const element = useContext(elementContext);
const ref = useRef();

useEffect(() => {
const element = ref.current;

if (!element) {
return;
}
Expand Down Expand Up @@ -77,7 +73,7 @@ export default function DeselectActiveControlOnClickOutside() {
return () => {
doc.removeEventListener('click', maybeDeselect);
};
}, [element]);
}, [ref]);

return null;
return <link ref={ref} />;
}
2 changes: 1 addition & 1 deletion build/editor-constructor-rtl.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/editor-constructor.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins'), 'version' => '108532761d7eb067483d');
<?php return array('dependencies' => array('lodash', 'react', 'react-dom', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins'), 'version' => '8428524b781fb2c4675e');
Loading
Loading