From c453036517bb6a068231d5844d5e173f4b7c6759 Mon Sep 17 00:00:00 2001 From: xylk Date: Wed, 22 Feb 2017 12:06:30 -0500 Subject: [PATCH] support mapping string values --- README.md | 6 ++-- src/functions.js | 3 ++ src/index.js | 2 +- src/keysMaps.js | 44 ------------------------- src/maps.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 90 insertions(+), 48 deletions(-) delete mode 100644 src/keysMaps.js create mode 100644 src/maps.js diff --git a/README.md b/README.md index 3c9c442..d7331ba 100644 --- a/README.md +++ b/README.md @@ -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()) @@ -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() diff --git a/src/functions.js b/src/functions.js index 3d8d1a9..2a4ced0 100644 --- a/src/functions.js +++ b/src/functions.js @@ -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)) : diff --git a/src/index.js b/src/index.js index a93f011..b74b091 100644 --- a/src/index.js +++ b/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) diff --git a/src/keysMaps.js b/src/keysMaps.js deleted file mode 100644 index 7029e1e..0000000 --- a/src/keysMaps.js +++ /dev/null @@ -1,44 +0,0 @@ -const markKeysMap = { - type: ['t'], - attrs: ['a'], -} - -const contentKeysMap = { - type: ['t'], - text: ['te'], - attrs: ['a'], - 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], -} diff --git a/src/maps.js b/src/maps.js new file mode 100644 index 0000000..d992cf2 --- /dev/null +++ b/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], +}