Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1a96335

Browse files
committed
Merge branch 'master' of https://github.com/sstur/react-rte
0.10.1
2 parents 1299bf9 + 6164051 commit 1a96335

File tree

5 files changed

+54
-14
lines changed

5 files changed

+54
-14
lines changed

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-rte-image",
3-
"version": "0.10.0",
3+
"version": "0.10.1",
44
"description": "React Rich Text Editor",
55
"main": "dist/react-rte.js",
66
"scripts": {

‎src/RichTextEditor.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import EditorToolbar from './lib/EditorToolbar';
1212
import EditorValue from './lib/EditorValue';
1313
import LinkDecorator from './lib/LinkDecorator';
1414
import ImageDecorator from './lib/ImageDecorator';
15+
import composite from './lib/composite';
1516
import cx from 'classnames';
1617
import autobind from 'class-autobind';
1718
import EventEmitter from 'events';
@@ -22,7 +23,7 @@ import styles from './RichTextEditor.css';
2223

2324
import type {ContentBlock, Entity} from 'draft-js';
2425
import type {ToolbarConfig} from './lib/EditorToolbarConfig';
25-
import type {Options} from './lib/EditorValue';
26+
import type {ImportOptions} from './lib/EditorValue';
2627

2728
const MAX_LIST_DEPTH = 2;
2829

@@ -50,6 +51,7 @@ type Props = {
5051
readOnly?: boolean;
5152
disabled?: boolean; // Alias of readOnly
5253
toolbarConfig?: ToolbarConfig;
54+
blockStyleFn?: (block: ContentBlock) => ?string;
5355
};
5456

5557
export default class RichTextEditor extends Component {
@@ -73,6 +75,7 @@ export default class RichTextEditor extends Component {
7375
readOnly,
7476
disabled,
7577
toolbarConfig,
78+
blockStyleFn,
7679
...otherProps // eslint-disable-line comma-dangle
7780
} = this.props;
7881
let editorState = value.getEditorState();
@@ -106,7 +109,7 @@ export default class RichTextEditor extends Component {
106109
<div className={combinedEditorClassName}>
107110
<Editor
108111
{...otherProps}
109-
blockStyleFn={getBlockStyle}
112+
blockStyleFn={composite(defaultBlockStyleFn,blockStyleFn)}
110113
customStyleMap={customStyleMap}
111114
editorState={editorState}
112115
handleReturn={this._handleReturn}
@@ -302,7 +305,7 @@ export default class RichTextEditor extends Component {
302305
}
303306
}
304307

305-
function getBlockStyle(block: ContentBlock): string {
308+
function defaultBlockStyleFn(block: ContentBlock): string {
306309
let result = styles.block;
307310
switch (block.getType()) {
308311
case 'unstyled':
@@ -322,7 +325,7 @@ function createEmptyValue(): EditorValue {
322325
return EditorValue.createEmpty(decorator);
323326
}
324327

325-
function createValueFromString(markup: string, format: string, options?: Options): EditorValue {
328+
function createValueFromString(markup: string, format: string, options?: ImportOptions): EditorValue {
326329
return EditorValue.createFromString(markup, format, decorator, options);
327330
}
328331

‎src/lib/EditorValue.js‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import {stateToMarkdown} from 'draft-js-export-markdown';
66
import {stateFromMarkdown} from 'draft-js-import-markdown';
77

88
import type {DraftDecoratorType as Decorator} from 'draft-js/lib/DraftDecoratorType';
9-
import type {Options} from 'draft-js-import-html';
10-
export type {Options};
9+
import type {Options as ImportOptions} from 'draft-js-import-html';
10+
import type {Options as ExportOptions} from 'draft-js-export-html';
11+
export type {ImportOptions, ExportOptions};
1112

1213
type StringMap = {[key: string]: string};
1314

@@ -30,15 +31,15 @@ export default class EditorValue {
3031
new EditorValue(editorState);
3132
}
3233

33-
toString(format: string): string {
34+
toString(format: string,options?: ExportOptions): string {
3435
let fromCache = this._cache[format];
3536
if (fromCache != null) {
3637
return fromCache;
3738
}
38-
return (this._cache[format] = toString(this.getEditorState(), format));
39+
return (this._cache[format] = toString(this.getEditorState(), format,options));
3940
}
4041

41-
setContentFromString(markup: string, format: string, options?: Options): EditorValue {
42+
setContentFromString(markup: string, format: string, options?: ImportOptions): EditorValue {
4243
let editorState = EditorState.push(
4344
this._editorState,
4445
fromString(markup, format, options),
@@ -56,18 +57,18 @@ export default class EditorValue {
5657
return new EditorValue(editorState);
5758
}
5859

59-
static createFromString(markup: string, format: string, decorator: ?Decorator, options?: Options): EditorValue {
60+
static createFromString(markup: string, format: string, decorator: ?Decorator, options?: ImportOptions): EditorValue {
6061
let contentState = fromString(markup, format, options);
6162
let editorState = EditorState.createWithContent(contentState, decorator);
6263
return new EditorValue(editorState, {[format]: markup});
6364
}
6465
}
6566

66-
function toString(editorState: EditorState, format: string): string {
67+
function toString(editorState: EditorState, format: string,options?: ExportOptions): string {
6768
let contentState = editorState.getCurrentContent();
6869
switch (format) {
6970
case 'html': {
70-
return stateToHTML(contentState);
71+
return stateToHTML(contentState,options);
7172
}
7273
case 'markdown': {
7374
return stateToMarkdown(contentState);
@@ -78,7 +79,7 @@ function toString(editorState: EditorState, format: string): string {
7879
}
7980
}
8081

81-
function fromString(markup: string, format: string, options?: Options): ContentState {
82+
function fromString(markup: string, format: string, options?: ImportOptions): ContentState {
8283
switch (format) {
8384
case 'html': {
8485
return stateFromHTML(markup, options);

‎src/lib/__tests__/composite-test.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @flow
2+
const {describe, it} = global;
3+
4+
import composite from '../composite';
5+
import expect from 'expect';
6+
7+
describe('composite', () => {
8+
it('should return the composite of two functions', () => {
9+
let addOne = (x) => x + 1;
10+
let addTwo = (x) => x + 2;
11+
expect(
12+
composite(addOne, addTwo)(5)
13+
).toBe(7);
14+
expect(
15+
composite(addOne, undefined)(5)
16+
).toBe(6);
17+
});
18+
});

‎src/lib/composite.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @flow
2+
3+
function composite<T, U>(
4+
defaultFunc: (input: T) => U,
5+
customFunc?: (input: T) => ?U,
6+
): (input: T) => U {
7+
return (input: T) => {
8+
if (customFunc) {
9+
let result = customFunc(input);
10+
if (result != null) {
11+
return result;
12+
}
13+
}
14+
return defaultFunc(input);
15+
};
16+
}
17+
18+
export default composite;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /