Skip to content
This repository has been archived by the owner on Feb 7, 2025. It is now read-only.

fix(ui-markdown-editor): styleblock refresh - #276 #283

Merged
merged 1 commit into from
Mar 8, 2021
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
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import React from 'react';
import PropTypes from 'prop-types';
import { Node } from 'slate';
import { useEditor } from 'slate-react';
import { Dropdown } from 'semantic-ui-react';
import StyleDropdownItem from './Item';
import {
BLOCK_STYLE,
DROPDOWN_STYLE,
DROPDOWN_STYLE_H1,
DROPDOWN_STYLE_H2,
Expand All @@ -15,11 +13,9 @@ import {
PARAGRAPH, H1, H2, H3
} from '../../utilities/schema';

const StyleDropdown = ({ canBeFormatted }) => {
const StyleDropdown = ({ canBeFormatted, currentStyle }) => {
const editor = useEditor();
const currentBlock = (editor && editor.selection)
? BLOCK_STYLE[Node.parent(editor, editor.selection.focus.path).type]
: 'Style';
const currentBlock = currentStyle;
return (
<Dropdown
simple
Expand Down Expand Up @@ -57,7 +53,8 @@ const StyleDropdown = ({ canBeFormatted }) => {
};

StyleDropdown.propTypes = {
canBeFormatted: PropTypes.func
canBeFormatted: PropTypes.func,
currentStyle: PropTypes.string,
};

export default StyleDropdown;
6 changes: 4 additions & 2 deletions packages/ui-markdown-editor/src/FormattingToolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const FormattingToolbar = ({
canBeFormatted,
showLinkModal,
setShowLinkModal,
activeButton
activeButton,
currentStyle
}) => {
const editor = useEditor();
const linkModalRef = useRef();
Expand Down Expand Up @@ -88,7 +89,7 @@ const FormattingToolbar = ({

return (
<ToolbarMenu id="ap-rich-text-editor-toolbar">
<StyleDropdown canBeFormatted={canBeFormatted}/>
<StyleDropdown canBeFormatted={canBeFormatted} currentStyle={currentStyle}/>
<Separator />
<FormatButton {...mark} {...bold} {...buttonProps} />
<FormatButton {...mark} {...italic} {...buttonProps} />
Expand All @@ -114,6 +115,7 @@ FormattingToolbar.propTypes = {
showLinkModal: PropTypes.bool,
setShowLinkModal: PropTypes.func,
activeButton: PropTypes.object,
currentStyle: PropTypes.string,
};


Expand Down
10 changes: 8 additions & 2 deletions packages/ui-markdown-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { HtmlTransformer } from '@accordproject/markdown-html';
import { SlateTransformer } from '@accordproject/markdown-slate';
import isHotkey from 'is-hotkey';
import { Editable, withReact, Slate, ReactEditor } from 'slate-react';
import { Editor, Range, Node, createEditor, Transforms } from 'slate';
import { Editor, Range, createEditor, Transforms, Node } from 'slate';
import { withHistory } from 'slate-history';
import PropTypes from 'prop-types';
import HOTKEYS, { formattingHotKeys } from './utilities/hotkeys';
import { BUTTON_ACTIVE } from './utilities/constants';
import { BUTTON_ACTIVE, BLOCK_STYLE } from './utilities/constants';
import withSchema from './utilities/schema';
import Element from './components';
import Leaf from './components/Leaf';
Expand All @@ -37,6 +37,7 @@ export const MarkdownEditor = (props) => {
canBeFormatted
} = props;
const [showLinkModal, setShowLinkModal] = useState(false);
const [currentStyle, setCurrentStyle] = useState('')
const editor = useMemo(() => {
if (augmentEditor) {
return augmentEditor(
Expand Down Expand Up @@ -145,6 +146,8 @@ export const MarkdownEditor = (props) => {
if (selection && isSelectionLinkBody(editor)) {
setShowLinkModal(true);
}
const currentStyleCalculated = BLOCK_STYLE[Node.parent(editor, editor.selection.focus.path).type] || 'Style';
setCurrentStyle(currentStyleCalculated);
};

const handleDragStart = (event) => {
Expand Down Expand Up @@ -179,6 +182,7 @@ export const MarkdownEditor = (props) => {
<Slate editor={editor} value={props.value} onChange={onChange} >
{ !props.readOnly
&& <FormatBar
currentStyle={currentStyle}
canBeFormatted={props.canBeFormatted}
showLinkModal={showLinkModal}
setShowLinkModal={setShowLinkModal}
Expand Down Expand Up @@ -242,6 +246,8 @@ MarkdownEditor.propTypes = {
onDrop: PropTypes.func,
/* Optional function to call when onDragOver event fires which will receive editor and event */
onDragOver: PropTypes.func,
/* Tells the current style of text block that the cursor is on and updates onChange */
currentStyle: PropTypes.string,
};

MarkdownEditor.defaultProps = {
Expand Down