Last active
March 20, 2022 11:03
-
-
Save ShaMan123/6c5c4ca2cc720a2700848a2deb6addcd to your computer and use it in GitHub Desktop.
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
function migrateObject(obj) { | |
const clipPath = obj?.clipPath; | |
if (clipPath && clipPath.type === 'group' && clipPath.eraser === true) { | |
clipPath.type = 'eraser'; | |
delete clipPath.eraser; | |
const rect = clipPath.objects.shift(); | |
obj.clipPath = rect.clipPath; | |
obj.eraser = clipPath; | |
} | |
} | |
function traverseTree(target) { | |
target.objects?.forEach(obj => { | |
migrateObject(obj); | |
migrateObject(obj.clipPath); | |
traverseTree(obj); | |
}); | |
} | |
/** | |
* call this method | |
* @param {Object} canvasJSON | |
* @returns {Object} version 2 compatible json | |
*/ | |
function migrate(canvasJSON) { | |
traverseTree(canvasJSON); | |
['backgroundImage', 'overlayImage'].forEach(key => { | |
if (canvasJSON[key]) { | |
migrateObject(canvasJSON[key]); | |
traverseTree(canvasJSON[key]); | |
} | |
}); | |
return canvasJSON; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Call
migrate
with the json payload.It returns a migrated payload.
It's safe to run on v2 payload if you're not sure if your payload is up to date or not.