Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Oct 30, 2018
1 parent a7116e1 commit d30fc17
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 273 deletions.
1 change: 1 addition & 0 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ function gutenberg_register_scripts_and_styles() {
'lodash',
'wp-polyfill',
'wp-data',
'wp-deprecated',
'wp-escape-html',
),
filemtime( gutenberg_dir_path() . 'build/rich-text/index.js' ),
Expand Down
36 changes: 27 additions & 9 deletions packages/editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ import {
getTextContent,
insert,
insertLineSeparator,
removeNextLineSeparator,
removePreviousLineSeparator,
isEmptyLine,
unstableToDom,
getSelectionStart,
getSelectionEnd,
charAt,
LINE_SEPARATOR,
remove,
isCollapsed,
} from '@wordpress/rich-text';
import { decodeEntities } from '@wordpress/html-entities';

Expand Down Expand Up @@ -515,10 +519,8 @@ export class RichText extends Component {
const { keyCode } = event;
const isReverse = keyCode === BACKSPACE;

const { isCollapsed } = getSelection();

// Only process delete if the key press occurs at uncollapsed edge.
if ( ! isCollapsed ) {
if ( ! getSelection().isCollapsed ) {
return;
}

Expand Down Expand Up @@ -613,13 +615,29 @@ export class RichText extends Component {

if ( keyCode === DELETE || keyCode === BACKSPACE ) {
if ( this.multilineTag ) {
const record = this.createRecord();
const value = this.createRecord();
const start = getSelectionStart( value );
const end = getSelectionEnd( value );

let newValue;

if ( keyCode === BACKSPACE ) {
newValue = removePreviousLineSeparator( record );
} else {
newValue = removeNextLineSeparator( record );
if ( charAt( value, start - 1 ) === LINE_SEPARATOR ) {
newValue = remove(
value,
// Only remove the line if the selection is
// collapsed.
isCollapsed( value ) ? start - 1 : start,
end
);
}
} else if ( charAt( value, end ) === LINE_SEPARATOR ) {
newValue = remove(
value,
start,
// Only remove the line if the selection is collapsed.
isCollapsed( value ) ? end + 1 : end,
);
}

if ( newValue ) {
Expand Down
35 changes: 28 additions & 7 deletions packages/rich-text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,44 @@ _This package assumes that your code will run in an **ES2015+** environment. If
### create

```js
create( ?input: Element | string, ?range: Range, ?multilineTag: string, ?settings: Object ): Object
```

Create a RichText value from an `Element` tree (DOM), an HTML string or a plain text string, with optionally a `Range` object to set the selection. If called without a given `input`, an empty value will be created. If `multilineTag` is provided, any content of direct children whose type matches `multilineTag` will be separated by two newlines. The `settings` object can be used to filter out content.
create( {
?element: Element,
?text: string,
?html: string,
?range: Range,
?multilineTag: string,
?multilineWrapperTags: Array,
?removeNode: Function,
?unwrapNode: Function,
?filterString: Function,
?removeAttribute: Function,
} ): Object
```

Create a RichText value from an `Element` tree (DOM), an HTML string or a plain text string, with optionally a `Range` object to set the selection. If called without any arguments, an empty value will be created. If `multilineTag` is provided, any content of direct children whose type matches `multilineTag` will be separated by a line separator. The remaining parameters can be used to filter out content.

### toHTMLString

```js
toHTMLString( value: Object, ?multilineTag: string ): string
toHTMLString( {
value: Object,
?multilineTag: string,
?multilineWrapperTags: Array,
} ): string
```

Create an HTML string from a Rich Text value. If a `multilineTag` is provided, text separated by two new lines will be wrapped in it.
Create an HTML string from a Rich Text value. If a `multilineTag` is provided, text separated by a line separator will be wrapped in it.

### apply

```js
apply( value: Object, current: Element, ?multilineTag ): void
apply( {
value: Object,
current: Element,
?multilineTag: string
?multilineWrapperTags: Array,
?createLinePadding: Function,
} ): void
```

Create an `Element` tree from a Rich Text value and applies the difference to the `Element` tree contained by `current`. If a `multilineTag` is provided, text separated by two new lines will be wrapped in an `Element` of that type.
Expand Down
1 change: 1 addition & 0 deletions packages/rich-text/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"@babel/runtime": "^7.0.0",
"@wordpress/data": "file:../data",
"@wordpress/deprecated": "file:../deprecated",
"@wordpress/escape-html": "file:../escape-html",
"lodash": "^4.17.10",
"rememo": "^3.0.0"
Expand Down
12 changes: 12 additions & 0 deletions packages/rich-text/src/char-at.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Gets the character at the specified index, or returns `undefined` if no
* character was found.
*
* @param {Object} value Value to get the character from.
* @param {string} index Index to use.
*
* @return {?string} A one character long string, or undefined.
*/
export function charAt( { text }, index ) {
return text[ index ];
}
12 changes: 12 additions & 0 deletions packages/rich-text/src/get-selection-end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Gets the end index of the current selection, or returns `undefined` if no
* selection exists. The selection ends right before the character at this
* index.
*
* @param {Object} value Value to get the selection from.
*
* @return {?number} Index where the selection ends.
*/
export function getSelectionEnd( { end } ) {
return end;
}
12 changes: 12 additions & 0 deletions packages/rich-text/src/get-selection-start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Gets the start index of the current selection, or returns `undefined` if no
* selection exists. The selection starts right before the character at this
* index.
*
* @param {Object} value Value to get the selection from.
*
* @return {?number} Index where the selection starts.
*/
export function getSelectionStart( { start } ) {
return start;
}
5 changes: 3 additions & 2 deletions packages/rich-text/src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import './store';

export { applyFormat } from './apply-format';
export { charAt } from './char-at';
export { concat } from './concat';
export { create } from './create';
export { getActiveFormat } from './get-active-format';
export { getFormatType } from './get-format-type';
export { getFormatTypes } from './get-format-types';
export { getSelectionEnd } from './get-selection-end';
export { getSelectionStart } from './get-selection-start';
export { getTextContent } from './get-text-content';
export { isCollapsed } from './is-collapsed';
export { isEmpty, isEmptyLine } from './is-empty';
export { join } from './join';
export { registerFormatType } from './register-format-type';
export { removeFormat } from './remove-format';
export { remove } from './remove';
export { removeNextLineSeparator } from './remove-next-line-separator.js';
export { removePreviousLineSeparator } from './remove-previous-line-separator.js';
export { replace } from './replace';
export { insert } from './insert';
export { insertLineSeparator } from './insert-line-separator';
Expand Down
35 changes: 0 additions & 35 deletions packages/rich-text/src/remove-next-line-separator.js

This file was deleted.

35 changes: 0 additions & 35 deletions packages/rich-text/src/remove-previous-line-separator.js

This file was deleted.

88 changes: 0 additions & 88 deletions packages/rich-text/src/test/remove-next-line-separator.js

This file was deleted.

Loading

0 comments on commit d30fc17

Please sign in to comment.