Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Added usePluginToolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed Aug 5, 2020
1 parent e31bec6 commit 772fdd9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
25 changes: 5 additions & 20 deletions src/editor/plugins/Footnote/FootnoteButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import SidebarPopup from 'volto-slate/futurevolto/SidebarPopup';
import { Icon as VoltoIcon } from '@plone/volto/components';
import tagSVG from '@plone/volto/icons/tag.svg';
import briefcaseSVG from '@plone/volto/icons/briefcase.svg';
import formatClearSVG from '@plone/volto/icons/format-clear.svg';
// import formatClearSVG from '@plone/volto/icons/format-clear.svg';
import { Icon } from '@plone/volto/components';

import InlineForm from 'volto-slate/futurevolto/InlineForm';
Expand All @@ -20,8 +20,9 @@ import { useIntl, defineMessages } from 'react-intl';
import checkSVG from '@plone/volto/icons/check.svg';
import clearSVG from '@plone/volto/icons/clear.svg';
import editingSVG from '@plone/volto/icons/editing.svg';
import usePluginToolbar from 'volto-slate/editor/usePluginToolbar';

import { isEqual } from 'lodash';
// import { isEqual } from 'lodash';

import './less/editor.less';

Expand Down Expand Up @@ -132,8 +133,6 @@ const FootnoteButton = () => {

const isFootnote = isActiveFootnote(editor);

const footnoteRef = React.useRef(null);

const [showEditForm, setShowEditForm] = React.useState(false);
const [selection, setSelection] = React.useState(null);
const [formData, setFormData] = React.useState({});
Expand All @@ -151,7 +150,7 @@ const FootnoteButton = () => {
const sel = selection; // should we save selection?
if (Range.isRange(sel)) {
Transforms.select(editor, sel);
insertFootnote(editor, { ...formData });;
insertFootnote(editor, { ...formData });
} else {
Transforms.deselect(editor);
}
Expand Down Expand Up @@ -204,21 +203,7 @@ const FootnoteButton = () => {
[editor, intl, setAndSaveSelection, showEditForm],
);

const footnote = getActiveFootnote(editor);

const { setPluginToolbar } = editor;

React.useEffect(() => {
if (isFootnote && !isEqual(footnote, footnoteRef.current)) {
footnoteRef.current = footnote;
if (footnoteRef.current) {
setPluginToolbar(PluginToolbar);
}
} else if (!isFootnote) {
footnoteRef.current = null;
setPluginToolbar(null);
}
}, [PluginToolbar, footnote, isFootnote, setPluginToolbar]);
usePluginToolbar(editor, isActiveFootnote, getActiveFootnote, PluginToolbar);

return (
<>
Expand Down
22 changes: 22 additions & 0 deletions src/editor/usePluginToolbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { isEqual } from 'lodash';
import React from 'react';

export default (editor, isActiveElement, getActiveElement, PluginToolbar) => {
const isActive = isActiveElement(editor);
const elementRef = React.useRef(null);
const element = getActiveElement(editor);

const { setPluginToolbar } = editor;

React.useEffect(() => {
if (isActive && !isEqual(element, elementRef.current)) {
elementRef.current = element;
if (elementRef.current) {
setPluginToolbar(PluginToolbar);
}
} else if (!isActive) {
elementRef.current = null;
setPluginToolbar(null);
}
}, [PluginToolbar, element, isActive, setPluginToolbar]);
};

0 comments on commit 772fdd9

Please sign in to comment.