Skip to content

Commit

Permalink
support mapping string values
Browse files Browse the repository at this point in the history
  • Loading branch information
xylk committed Feb 22, 2017
1 parent 2a02a49 commit c453036
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 48 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -4,7 +4,7 @@

### \[('compress' || 'uncompress') + ('State' || 'Selection' || 'Step') + 'JSON']()

Renames the keys. Maintains JSON structure.
Renames known keys and values. Maintains JSON structure.

```js
let compressedStateJSON = compressStateJSON(/*editorState*/.toJSON())
Expand All @@ -20,11 +20,11 @@ let steps = compressedStepsJSON.map(json => Step.fromJSON(/*schema*/, uncompress
#### uncompressed vs compressed
```json
{"selection":{"head":764,"anchor":764}},"doc":{"type":"doc","content":[{"type":"heading","attrs":
{"selection":{"head":0,"anchor":0}},"doc":{"type":"doc","content":[{"type":"heading","attrs":{"level":3},
```
```json
{"s":{"h":764,"a":764}},"d":{"t":"doc","c":[{"t":"heading","a":
{"s":{"h":0,"a":0}},"d":{"t":"d","c":[{"t":"h","a":{"l":3},
```
### compressStepsLossy()
Expand Down
3 changes: 3 additions & 0 deletions src/functions.js
Expand Up @@ -25,6 +25,9 @@ function invertKeysMap(keysMap) {

function mapKeys(keysMap, obj) {
return (
typeof obj === 'string' ?
(keysMap[obj] || [ obj ])[0]
:
Array.isArray(obj) ?
obj.map(mapKeys.bind(0, keysMap))
:
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
@@ -1,4 +1,4 @@
import { stateKeysMap, selectionKeysMap, stepKeysMap } from './keysMaps'
import { stateKeysMap, selectionKeysMap, stepKeysMap } from './maps'
import { keysMappers } from './functions'

export const [ compressStateJSON, uncompressStateJSON ] = keysMappers(stateKeysMap)
Expand Down
44 changes: 0 additions & 44 deletions src/keysMaps.js

This file was deleted.

83 changes: 83 additions & 0 deletions src/maps.js
@@ -0,0 +1,83 @@
const markTypeValuesMap = {
em: ['e'],
strong: ['s'],
link: ['l'],
code: ['c'],
}

const markAttrsKeysMap = {
href: ['h'], // link
title: ['t'], // link
}

const markKeysMap = {
type: ['t', markTypeValuesMap],
attrs: ['a', markAttrsKeysMap],
}

const contentTypeValuesMap = {
doc: ['d'],
paragraph: ['p'],
blockquote: ['b'],
horizontal_rule: ['h_r'],
heading: ['h'],
code_block: ['c_b'],
text: ['t'],
image: ['i'],
hard_break: ['h_b'],
ordered_list: ['o_l'],
bullet_list: ['b_l'],
list_item: ['l_i'],
table: ['ta'],
table_row: ['t_r'],
table_cell: ['t_c'],
}

const contentAttrsKeysMap = {
level: ['l'], // heading
src: ['s'], // image
alt: ['a'], // image
title: ['t'], // image
order: ['o'], // ordered_list
columns: ['c'], // table, table_row
}

const contentKeysMap = {
type: ['t', contentTypeValuesMap],
text: ['te'],
attrs: ['a', contentAttrsKeysMap],
level: ['l'],
marks: ['m', markKeysMap],
}
contentKeysMap.content = ['c', contentKeysMap]

const sliceKeysMap = {
content: ['c', contentKeysMap],
openLeft: ['oL'],
openRight: ['oR'],
}

export const stepKeysMap = {
stepType: ['sT'],
from: ['f'],
to: ['t'],
structure: ['st'],
insert: ['i'],
gapFrom: ['gF'],
gapTo: ['gT'],
slice: ['s', sliceKeysMap],
mark: ['m', markKeysMap],
}

export const selectionKeysMap = {
head: ['h'],
anchor: ['a'],
node: ['n'],
after: ['af'],
}

export const stateKeysMap = {
doc: ['d', contentKeysMap],
selection: ['s', selectionKeysMap],
storedMarks: ['sM', markKeysMap],
}

0 comments on commit c453036

Please sign in to comment.