-
Notifications
You must be signed in to change notification settings - Fork 841
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
[EuiMarkdownEditor] Allow more plugins to be excludable #7676
Merged
cee-chen
merged 5 commits into
elastic:main
from
sakurai-youhei:make-line-breaks-plugin-excludable
Apr 13, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7612d36
Add option to opt out of line-breaks plugin
sakurai-youhei 5f01ec1
Add changelog
sakurai-youhei caf13dd
Updated snippet
sakurai-youhei d04966a
Extend `exclude` API to allow for excluding even more plugins
cee-chen f297f6f
Update docs & changelog
cee-chen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
- Updated `getDefaultEuiMarkdownPlugins()` to allow excluding the following plugins in addition to `tooltip`: | ||
- `checkbox` | ||
- `linkValidator` | ||
- `lineBreaks` | ||
- `emoji` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 93 additions & 16 deletions
109
src-docs/src/views/markdown_editor/markdown_editor_no_plugins.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,112 @@ | ||
import React, { useState } from 'react'; | ||
import React, { useState, useMemo } from 'react'; | ||
|
||
import { | ||
EuiMarkdownEditor, | ||
getDefaultEuiMarkdownPlugins, | ||
EuiFlexGroup, | ||
EuiFlexItem, | ||
EuiSwitch, | ||
} from '../../../../src/components'; | ||
|
||
const initialContent = `## This is how we do it :smile: | ||
const initialContent = ` | ||
### tooltip | ||
|
||
In this example, we unregistered the built in **tooltip** plugin. So the button in the toolbar and the help syntax won't be displayed. | ||
|
||
And the following syntax no longer works. | ||
When disabled, the button in the toolbar and the help syntax won't be displayed, and the following syntax will no longer works. | ||
|
||
!{tooltip[anchor text](Tooltip content)} | ||
`; | ||
|
||
const { parsingPlugins, processingPlugins, uiPlugins } = | ||
getDefaultEuiMarkdownPlugins({ exclude: ['tooltip'] }); | ||
### checkbox | ||
|
||
When disabled, a EuiCheckbox will no longer render. | ||
|
||
- [ ] TODO: Disable some default plugins | ||
|
||
### emoji | ||
|
||
When disabled, text will render instead of an emoji. | ||
|
||
:smile: | ||
|
||
### linkValidator | ||
|
||
When disabled, all links will render as links instead of as text. | ||
|
||
[This is a sus link](file://) | ||
|
||
### lineBreaks | ||
|
||
When disabled, these sentence will be in the same line. | ||
When enabled, these sentences will be separated by a line break. | ||
|
||
Two blank lines in a row will create a new paragraph as usual. | ||
`; | ||
|
||
export default () => { | ||
const [value, setValue] = useState(initialContent); | ||
|
||
const [tooltip, excludeTooltips] = useState(false); | ||
const [checkbox, excludeCheckboxes] = useState(true); | ||
const [emoji, excludeEmojis] = useState(true); | ||
const [linkValidator, excludeLinkValidator] = useState(true); | ||
const [lineBreaks, excludeLineBreaks] = useState(false); | ||
|
||
const { parsingPlugins, processingPlugins, uiPlugins } = useMemo(() => { | ||
const excludedPlugins = []; | ||
if (!tooltip) excludedPlugins.push('tooltip'); | ||
if (!checkbox) excludedPlugins.push('checkbox'); | ||
if (!emoji) excludedPlugins.push('emoji'); | ||
if (!linkValidator) excludedPlugins.push('linkValidator'); | ||
if (!lineBreaks) excludedPlugins.push('lineBreaks'); | ||
|
||
return getDefaultEuiMarkdownPlugins({ | ||
exclude: excludedPlugins, | ||
}); | ||
}, [tooltip, checkbox, emoji, linkValidator, lineBreaks]); | ||
|
||
return ( | ||
<> | ||
<EuiMarkdownEditor | ||
aria-label="EUI markdown editor with no default plugins demo" | ||
value={value} | ||
onChange={setValue} | ||
parsingPluginList={parsingPlugins} | ||
processingPluginList={processingPlugins} | ||
uiPlugins={uiPlugins} | ||
/> | ||
<EuiFlexGroup> | ||
<EuiFlexItem grow={false} css={{ gap: 20 }}> | ||
<EuiSwitch | ||
label="tooltip" | ||
checked={tooltip} | ||
onChange={() => excludeTooltips(!tooltip)} | ||
/> | ||
<EuiSwitch | ||
label="checkbox" | ||
checked={checkbox} | ||
onChange={() => excludeCheckboxes(!checkbox)} | ||
/> | ||
<EuiSwitch | ||
label="emoji" | ||
checked={emoji} | ||
onChange={() => excludeEmojis(!emoji)} | ||
/> | ||
<EuiSwitch | ||
label="linkValidator" | ||
checked={linkValidator} | ||
onChange={() => excludeLinkValidator(!linkValidator)} | ||
/> | ||
<EuiSwitch | ||
label="lineBreaks" | ||
checked={lineBreaks} | ||
onChange={() => excludeLineBreaks(!lineBreaks)} | ||
/> | ||
</EuiFlexItem> | ||
<EuiFlexItem> | ||
<EuiMarkdownEditor | ||
aria-label="Demo with excluded default plugins" | ||
value={value} | ||
onChange={setValue} | ||
parsingPluginList={parsingPlugins} | ||
processingPluginList={processingPlugins} | ||
uiPlugins={uiPlugins} | ||
initialViewMode="viewing" | ||
autoExpandPreview={false} | ||
height={400} | ||
/> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/components/markdown_editor/plugins/markdown_default_plugins/plugins.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { getDefaultEuiMarkdownPlugins } from './plugins'; | ||
|
||
describe('default plugins', () => { | ||
test('no exclusions', () => { | ||
const { parsingPlugins, processingPlugins, uiPlugins } = | ||
getDefaultEuiMarkdownPlugins(); | ||
|
||
expect(parsingPlugins).toHaveLength(7); | ||
expect(Object.keys(processingPlugins[1][1].components)).toHaveLength(8); | ||
expect(uiPlugins).toHaveLength(1); | ||
|
||
expect(processingPlugins[1][1].components.tooltipPlugin).toBeDefined(); | ||
expect(processingPlugins[1][1].components.checkboxPlugin).toBeDefined(); | ||
}); | ||
|
||
test('exclude tooltips', () => { | ||
const { parsingPlugins, processingPlugins, uiPlugins } = | ||
getDefaultEuiMarkdownPlugins({ | ||
exclude: ['tooltip'], | ||
}); | ||
|
||
expect(parsingPlugins).toHaveLength(6); | ||
expect(processingPlugins[1][1].components.tooltipPlugin).toBeUndefined(); | ||
expect(uiPlugins).toHaveLength(0); | ||
}); | ||
|
||
test('exclude checkboxes', () => { | ||
const { parsingPlugins, processingPlugins, uiPlugins } = | ||
getDefaultEuiMarkdownPlugins({ | ||
exclude: ['checkbox'], | ||
}); | ||
|
||
expect(parsingPlugins).toHaveLength(6); | ||
expect(processingPlugins[1][1].components.checkboxPlugin).toBeUndefined(); | ||
expect(uiPlugins).toHaveLength(1); | ||
}); | ||
|
||
test('all exclusions', () => { | ||
const { parsingPlugins, processingPlugins, uiPlugins } = | ||
getDefaultEuiMarkdownPlugins({ | ||
exclude: [ | ||
'tooltip', | ||
'checkbox', | ||
'lineBreaks', | ||
'linkValidator', | ||
'emoji', | ||
], | ||
}); | ||
|
||
expect(parsingPlugins).toHaveLength(2); | ||
expect(Object.keys(processingPlugins[1][1].components)).toHaveLength(6); | ||
expect(uiPlugins).toHaveLength(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[continuing to think out loud even more] Even more pie in the sky, but I think this map/setup also gives us the potential to allow consumers to configure the default plugin settings someday! That way if they do want emoticons for example, they could pass in a config object instead of having to turn it off completely and re-pass in the plugin.
But, that's very out of scope for this PR and no one's asking for it so I'm definitely not going to do it now 😆