Skip to content
This repository has been archived by the owner on May 19, 2020. It is now read-only.

Commit

Permalink
Merge pull request #185 from accordproject/dl-fix-toggle-style
Browse files Browse the repository at this point in the history
fix(*): fix toggling of bold and italics
  • Loading branch information
DianaLease authored Nov 8, 2019
2 parents 04cdb1f + bf320c8 commit ba16ac5
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 49 deletions.
5 changes: 0 additions & 5 deletions examples/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ import SlateAsInputEditor from '../../src/SlateAsInputEditor';

import * as serviceWorker from './serviceWorker';
import 'semantic-ui-css/semantic.min.css';
import NoEdit from '../../src/plugins/noedit';
import List from '../../src/plugins/list';

const plugins = [NoEdit(), List()];
const slateTransformer = new SlateTransformer();

const defaultMarkdown = `# My Heading
Expand Down Expand Up @@ -95,7 +92,6 @@ function Demo() {
<Grid.Column>
<MarkdownAsInputEditor
readOnly={true}
plugins={plugins}
markdown={markdown}
onChange={onMarkdownChange}
/>
Expand All @@ -105,7 +101,6 @@ function Demo() {
<SlateAsInputEditor
readOnly={false}
lockText={true}
plugins={plugins}
value={slateValue}
onChange={onSlateValueChange}
editorProps={propsObj}
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@
"slate-plain-serializer": "^0.7.10",
"style-loader": "^0.23.1"
}
}
}
3 changes: 3 additions & 0 deletions src/PluginManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export default class PluginManager {
* @param {string} code the type of edit requested
*/
isEditable(value, code) {
// if no plugins have an `isEditable` method, return true
if (!this.plugins.filter(plugin => plugin.isEditable).length) return true;

for (let n = 0; n < this.plugins.length; n += 1) {
const plugin = this.plugins[n];
if (plugin.isEditable && plugin.isEditable(value, code)) {
Expand Down
27 changes: 13 additions & 14 deletions src/SlateAsInputEditor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import baseSchema from '../schema';
import PluginManager from '../PluginManager';
import { FromHTML } from '../html/fromHTML';
import FormatToolbar from '../FormattingToolbar';
import NoEditPlugin from '../plugins/noedit';
import ListPlugin from '../plugins/list';

import '../styles.css';
Expand Down Expand Up @@ -114,11 +113,17 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
* Destructure props for efficiency
*/
const {
onChange, plugins, value, lockText
onChange, value, lockText
} = props;

const editorProps = props.editorProps || Object.create(null);

const plugins = React.useMemo(() => (props.plugins
? props.plugins.concat(
[ListPlugin()]
)
: [ListPlugin()]), [props.plugins]);

/**
* A reference to the Slate Editor.
*/
Expand Down Expand Up @@ -284,12 +289,12 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
const isEditable = useCallback((editor, code) => {
if (isEditorLockText(editor)) {
const { value } = editor;
const pluginManager = new PluginManager(props.plugins);
const pluginManager = new PluginManager(plugins);
return pluginManager.isEditable(value, code);
}

return true;
}, [isEditorLockText, props.plugins]);
}, [isEditorLockText, plugins]);

/**
* On backspace, if at the start of a non-paragraph, convert it back into a
Expand Down Expand Up @@ -395,7 +400,7 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
if (isEditable(editor, 'paste')) {
const transfer = getEventTransfer(event);
if (transfer.type === 'html') {
const pluginManager = new PluginManager(props.plugins);
const pluginManager = new PluginManager(plugins);
const fromHtml = new FromHTML(pluginManager);
// @ts-ignore
const { document } = fromHtml.convert(editor, transfer.html);
Expand Down Expand Up @@ -426,7 +431,7 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
*/
const renderEditor = useCallback((props, editor, next) => {
const children = next();
const pluginManager = new PluginManager(props.plugins);
const pluginManager = new PluginManager(plugins);

return (
<div>
Expand All @@ -439,7 +444,7 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
{children}
</div>
);
}, [editorProps]);
}, [editorProps, plugins]);

const onChangeHandler = ({ value }) => {
onChange(value);
Expand All @@ -450,12 +455,6 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
setTimeout(editor.focus, 0);
};

const allPlugins = React.useMemo(() => (props.plugins
? props.plugins.concat(
[ListPlugin(), NoEditPlugin()]
)
: [ListPlugin(), NoEditPlugin()]), [props.plugins]);

return (
<div>
<ToolbarWrapper {...editorProps} id="slate-toolbar-wrapper-id" />
Expand All @@ -469,7 +468,7 @@ const SlateAsInputEditor = React.forwardRef((props, ref) => {
onChange={onChangeHandler}
onFocus={onFocusHandler}
schema={slateSchema}
plugins={allPlugins}
plugins={plugins}
onBeforeInput={onBeforeInput}
onKeyDown={onKeyDown}
onPaste={onPaste}
Expand Down
3 changes: 1 addition & 2 deletions src/SlateAsInputEditor/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import Adapter from 'enzyme-adapter-react-16';
import { SlateTransformer } from '@accordproject/markdown-slate';
import SlateAsInputEditor from './index';

import NoEdit from '../plugins/noedit';
import List from '../plugins/list';

const plugins = [NoEdit(), List()];
const plugins = [List()];

Enzyme.configure({ adapter: new Adapter() });

Expand Down
26 changes: 0 additions & 26 deletions src/plugins/noedit.js

This file was deleted.

0 comments on commit ba16ac5

Please sign in to comment.