fix(widget-markdown): fix quote block and list highlighting (#5422)
This commit is contained in:
parent
5d288687f6
commit
b9624fc673
@ -108,6 +108,8 @@ export default class Toolbar extends React.Component {
|
|||||||
hasMark = () => {},
|
hasMark = () => {},
|
||||||
hasInline = () => {},
|
hasInline = () => {},
|
||||||
hasBlock = () => {},
|
hasBlock = () => {},
|
||||||
|
hasQuote = () => {},
|
||||||
|
hasListItems = () => {},
|
||||||
editorComponents,
|
editorComponents,
|
||||||
t,
|
t,
|
||||||
} = this.props;
|
} = this.props;
|
||||||
@ -211,7 +213,7 @@ export default class Toolbar extends React.Component {
|
|||||||
label={t('editor.editorWidgets.markdown.quote')}
|
label={t('editor.editorWidgets.markdown.quote')}
|
||||||
icon="quote"
|
icon="quote"
|
||||||
onClick={this.handleBlockClick}
|
onClick={this.handleBlockClick}
|
||||||
isActive={hasBlock('quote')}
|
isActive={hasQuote('quote')}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
@ -221,7 +223,7 @@ export default class Toolbar extends React.Component {
|
|||||||
label={t('editor.editorWidgets.markdown.bulletedList')}
|
label={t('editor.editorWidgets.markdown.bulletedList')}
|
||||||
icon="list-bulleted"
|
icon="list-bulleted"
|
||||||
onClick={this.handleBlockClick}
|
onClick={this.handleBlockClick}
|
||||||
isActive={hasBlock('bulleted-list')}
|
isActive={hasListItems('bulleted-list')}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
@ -231,7 +233,7 @@ export default class Toolbar extends React.Component {
|
|||||||
label={t('editor.editorWidgets.markdown.numberedList')}
|
label={t('editor.editorWidgets.markdown.numberedList')}
|
||||||
icon="list-numbered"
|
icon="list-numbered"
|
||||||
onClick={this.handleBlockClick}
|
onClick={this.handleBlockClick}
|
||||||
isActive={hasBlock('numbered-list')}
|
isActive={hasListItems('numbered-list')}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
@ -164,6 +164,8 @@ export default class Editor extends React.Component {
|
|||||||
hasMark = type => this.editor && this.editor.hasMark(type);
|
hasMark = type => this.editor && this.editor.hasMark(type);
|
||||||
hasInline = type => this.editor && this.editor.hasInline(type);
|
hasInline = type => this.editor && this.editor.hasInline(type);
|
||||||
hasBlock = type => this.editor && this.editor.hasBlock(type);
|
hasBlock = type => this.editor && this.editor.hasBlock(type);
|
||||||
|
hasQuote = type => this.editor && this.editor.hasQuote(type);
|
||||||
|
hasListItems = type => this.editor && this.editor.hasListItems(type);
|
||||||
|
|
||||||
handleToggleMode = () => {
|
handleToggleMode = () => {
|
||||||
this.props.onMode('raw');
|
this.props.onMode('raw');
|
||||||
@ -218,6 +220,8 @@ export default class Editor extends React.Component {
|
|||||||
hasMark={this.hasMark}
|
hasMark={this.hasMark}
|
||||||
hasInline={this.hasInline}
|
hasInline={this.hasInline}
|
||||||
hasBlock={this.hasBlock}
|
hasBlock={this.hasBlock}
|
||||||
|
hasQuote={this.hasQuote}
|
||||||
|
hasListItems={this.hasListItems}
|
||||||
isShowModeToggle={isShowModeToggle}
|
isShowModeToggle={isShowModeToggle}
|
||||||
t={t}
|
t={t}
|
||||||
disabled={isDisabled}
|
disabled={isDisabled}
|
||||||
|
@ -71,6 +71,39 @@ function CommandsAndQueries({ defaultType }) {
|
|||||||
hasInline(editor, type) {
|
hasInline(editor, type) {
|
||||||
return editor.value.inlines.some(node => node.type === type);
|
return editor.value.inlines.some(node => node.type === type);
|
||||||
},
|
},
|
||||||
|
hasQuote(editor, quoteLabel) {
|
||||||
|
const { value } = editor;
|
||||||
|
const { document, blocks } = value;
|
||||||
|
return blocks.some(node => {
|
||||||
|
const { key: descendantNodeKey } = node;
|
||||||
|
/* When focusing a quote block, the actual block that gets the focus is the paragraph block whose parent is a `quote` block.
|
||||||
|
Hence, we need to get its parent and check if its type is `quote`. This parent will always be defined because every block in the editor
|
||||||
|
has a Document object as parent by default.
|
||||||
|
*/
|
||||||
|
const parent = document.getParent(descendantNodeKey);
|
||||||
|
return parent.type === quoteLabel;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
hasListItems(editor, listType) {
|
||||||
|
const { value } = editor;
|
||||||
|
const { document, blocks } = value;
|
||||||
|
return blocks.some(node => {
|
||||||
|
const { key: lowestNodeKey } = node;
|
||||||
|
/* A list block has the following structure:
|
||||||
|
<ol>
|
||||||
|
<li>
|
||||||
|
<p>Coffee</p>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<p>Tea</p>
|
||||||
|
</li>
|
||||||
|
</ol>
|
||||||
|
*/
|
||||||
|
const parent = document.getParent(lowestNodeKey);
|
||||||
|
const grandparent = document.getParent(parent.key);
|
||||||
|
return parent.type === 'list-item' && grandparent?.type === listType;
|
||||||
|
});
|
||||||
|
},
|
||||||
},
|
},
|
||||||
commands: {
|
commands: {
|
||||||
toggleBlock(editor, type) {
|
toggleBlock(editor, type) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user