Last active
June 2, 2017 15:33
-
-
Save gabrielrtakeda/ad4646ac01be7a6d2acb769d8fa69113 to your computer and use it in GitHub Desktop.
[Nossas] Slate Editor: state object migration from < v2.3 to v2.6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// requirements: | |
// - node.js version: >= v7.10.0 | |
// | |
// installation: | |
// - `mkdir slate-editor-state-object-migration` (make a new directory) | |
// - `yarn init` (initialize yarn) | |
// - `yarn add lodash json-stringify-pretty-compact escape-json-node --save` (install dependencies) | |
// - download the script using the command below: | |
// - curl -s https://gist.githubusercontent.com/gabrielrtakeda/ad4646ac01be7a6d2acb769d8fa69113/raw/ba832904dce897e092a7a9ed962d1cb9ad611613/slate-editor-state-object-migration.js > ./migration.js | |
// - update the `updateSQL` function with your database table data | |
// - `node ./migration.js > migration.sql` (execute the migration) | |
// - take the **migration.sql** file and execute it on your database | |
// | |
var _ = require('lodash') | |
var stringify = require('json-stringify-pretty-compact') | |
var escapeJSON = require('escape-json-node') | |
const deep = (obj, type) => { | |
var keys = [] | |
for(var key in obj) { | |
if (key === 'type' && obj[key] === type) { | |
keys.push(key) | |
} | |
if(typeof obj[key] === 'object') { | |
var subkeys = deep(obj[key], type) | |
keys = keys.concat(subkeys.map(subkey => `${key}.${subkey}`)) | |
} | |
} | |
return keys | |
} | |
const docs = [ | |
// list of slate-editor state object | |
// pattern: { id: 000, doc: { ...stateObject } } | |
] | |
const parent = path => path.split('.').slice(0, -1).join('.') | |
const legacy = doc => { | |
const links = deep(doc, 'link') | |
const legacyLinkPaths = [] | |
links.map(pathLink => { | |
const nodes = _.get(doc, `${parent(pathLink)}.nodes`) | |
nodes.map(node => { | |
const hasNestedImages = !!deep(node, 'image').length | |
if (hasNestedImages) { | |
legacyLinkPaths.push(pathLink) | |
} | |
}) | |
}) | |
return legacyLinkPaths | |
} | |
const merged = (doc, pathLinks) => pathLinks.map(pathLink => { | |
const nodes = _.get(doc, `${parent(pathLink)}.nodes`) | |
const link = _.get(doc, parent(pathLink)) | |
const linkData = link.data | |
let imageData | |
nodes.map(node => { | |
const imagePath = deep(link, 'image').join('') | |
if (imagePath) { | |
imageData = _.get(link, parent(imagePath)).data | |
} | |
}) | |
return _.merge(linkData, imageData) | |
}) | |
const template = dataList => dataList.map(data => ({ | |
"data": { | |
"href": data.href, | |
"title": data.title, | |
"openExternal": data.target === '_blank', | |
"src": data.src, | |
}, | |
"kind": "inline", | |
"isVoid": true, | |
"type": "imageLink", | |
"nodes": [ | |
{ | |
"kind": "text", | |
"ranges": [{ "kind": "range", "text": " ", "marks": [] }] | |
} | |
] | |
})) | |
const update = (doc, linkPaths, templates) => { | |
linkPaths.map((linkPath, i) => { | |
_.set(doc, parent(linkPath), templates[i]) | |
}) | |
return doc | |
} | |
const escapeIt = doc => | |
JSON.stringify(escapeJSON(JSON.stringify(doc))) | |
.replace(/\\\\\\\\\\/g, '\\\\\\') | |
.replace(/\$/g, '\\$') | |
const updateSQL = (id, escapedDoc) => | |
`UPDATE widgets SET settings = settings || ($$content => ${escapedDoc}$$) WHERE id = ${id};` | |
const print = (id, escapedDoc, linkPaths) => { | |
if (linkPaths.length) console.log(updateSQL(id, escapedDoc)) | |
} | |
docs.map(({ id, doc }) => { | |
const linkPaths = legacy(doc) | |
const dataList = merged(doc, linkPaths) | |
const templates = template(dataList) | |
const updateDoc = update(doc, linkPaths, templates) | |
const escapedDoc = escapeIt(updateDoc) | |
print(id, escapedDoc, linkPaths) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment