-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vectorizer): Simplify and move into worker
- Loading branch information
1 parent
5beb7c3
commit 1f17f48
Showing
12 changed files
with
312 additions
and
274 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "chrome", | ||
"request": "launch", | ||
"name": "Launch Chrome against localhost", | ||
"url": "http://localhost:5173", | ||
"webRoot": "${workspaceFolder}" | ||
} | ||
] | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import type CreativeEditorSDK from '@cesdk/cesdk-js'; | ||
|
||
import { | ||
getPluginMetadata, | ||
isMetadataConsistent, | ||
recoverInitialImageData, | ||
setPluginMetadata | ||
} from './utils'; | ||
|
||
|
||
const runInWorker = (uri: string) => new Promise<Blob>((resolve, reject) => { | ||
const worker = new Worker(new URL('./worker', import.meta.url), { type: 'module' }); | ||
worker.postMessage({data: uri}) | ||
worker.onmessage = (e: MessageEvent) => { | ||
const msg = e.data | ||
if (msg.error) { | ||
reject (msg.error) | ||
return; | ||
} | ||
resolve(new Blob([msg.data])) | ||
// when done terminate | ||
worker.terminate() | ||
} | ||
|
||
}) | ||
|
||
|
||
/** | ||
* Apply the vectorization process to the image. | ||
*/ | ||
|
||
/** | ||
* Triggers the vectiorize process. | ||
*/ | ||
export async function command( | ||
cesdk: CreativeEditorSDK, | ||
blockId: number | ||
) { | ||
const engine = cesdk.engine; // the only function that needs the ui is the upload function | ||
const blockApi = cesdk.engine.block; | ||
if (!blockApi.hasFill(blockId)) | ||
throw new Error('Block has no fill to vectorize'); | ||
|
||
const fillId = blockApi.getFill(blockId); | ||
|
||
// Get the current image URI and source set as initial values. | ||
const initialSourceSet = blockApi.getSourceSet( | ||
fillId, | ||
'fill/image/sourceSet' | ||
); | ||
const initialImageFileURI = blockApi.getString( | ||
fillId, | ||
'fill/image/imageFileURI' | ||
); | ||
const initialPreviewFileURI = blockApi.getString( | ||
fillId, | ||
'fill/image/previewFileURI' | ||
); | ||
|
||
|
||
const uriToProcess = | ||
// Source sets have priority in the engine | ||
initialSourceSet.length > 0 | ||
? // Choose the highest resolution image in the source set | ||
initialSourceSet.sort( | ||
(a, b) => b.width * b.height - a.height * a.width | ||
)[0].uri | ||
: initialImageFileURI; | ||
|
||
if (uriToProcess === undefined || uriToProcess === '') | ||
return; // We shall return early if the uri is not defined or invalid | ||
|
||
|
||
|
||
try { | ||
// Clear values in the engine to trigger the loading spinner | ||
+9 | ||
blockApi.setString(fillId, 'fill/image/imageFileURI', ''); | ||
blockApi.setSourceSet(fillId, 'fill/image/sourceSet', []); | ||
// ensure we show the last image while processsing. Some images don't have the preview set | ||
if (initialPreviewFileURI === undefined || initialPreviewFileURI === '') { | ||
blockApi.setString(fillId, 'fill/image/previewFileURI', uriToProcess); | ||
} | ||
const metadata = getPluginMetadata(engine, blockId); | ||
setPluginMetadata(engine, blockId, { | ||
...metadata, | ||
version: PLUGIN_VERSION, | ||
initialSourceSet, | ||
initialImageFileURI, | ||
blockId, | ||
fillId, | ||
status: 'PROCESSING' | ||
}); | ||
|
||
const vectorized: Blob = await runInWorker(uriToProcess) | ||
|
||
if ( | ||
getPluginMetadata(cesdk.engine, blockId).status !== 'PROCESSING' || | ||
!isMetadataConsistent(cesdk, blockId) | ||
) | ||
return; | ||
|
||
const pathname = new URL(uriToProcess).pathname; | ||
const parts = pathname.split('/'); | ||
const filename = parts[parts.length - 1]; | ||
|
||
const uploadedAssets = await cesdk.unstable_upload( | ||
new File([vectorized], filename, { type: vectorized.type }), | ||
() => { | ||
// TODO Delegate process to UI component | ||
} | ||
); | ||
|
||
// Check for externally changed state while we were uploading and | ||
// do not proceed if the state was reset. | ||
if ( | ||
getPluginMetadata(engine, blockId).status !== 'PROCESSING' || | ||
!isMetadataConsistent(cesdk, blockId) | ||
) | ||
return; | ||
|
||
const url = uploadedAssets.meta?.uri; | ||
if (url == null) { | ||
throw new Error('Could not upload vectorized image'); | ||
} | ||
|
||
setPluginMetadata(engine, blockId, { | ||
version: PLUGIN_VERSION, | ||
initialSourceSet, | ||
initialImageFileURI, | ||
blockId, | ||
fillId, | ||
status: 'PROCESSED_TOGGLE_ON', | ||
processedAsset: url | ||
}); | ||
blockApi.setString(fillId, 'fill/image/imageFileURI', url); | ||
// Finally, create an undo step | ||
cesdk.engine.editor.addUndoStep(); | ||
} catch (error) { | ||
if (cesdk.engine.block.isValid(blockId)) { | ||
setPluginMetadata(engine, blockId, { | ||
version: PLUGIN_VERSION, | ||
initialSourceSet, | ||
initialImageFileURI, | ||
blockId, | ||
fillId, | ||
status: 'ERROR' | ||
}); | ||
|
||
recoverInitialImageData(cesdk, blockId); | ||
} | ||
// eslint-disable-next-line no-console | ||
console.error(error); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
export const PLUGIN_ID = '@imgly/plugin-vectorizer-web'; | ||
export const CANVAS_MENU_COMPONENT_ID = `${PLUGIN_ID}.canvasMenu`; | ||
export const CANVAS_MENU_COMPONENT_BUTTON_ID = `${CANVAS_MENU_COMPONENT_ID}.button`; | ||
export const FEATURE_ID = `${PLUGIN_ID}.feature`; | ||
export const I18N_ID = "plugin.vectorizer.vectorize" | ||
export const I18N_TRANSLATIONS = { | ||
en: { [I18N_ID]: 'Vectorize' }, | ||
de: { [I18N_ID]: 'Vektorisieren' } | ||
export const PLUGIN_CANVAS_MENU_COMPONENT_ID = `${PLUGIN_ID}.canvasMenu`; | ||
export const PLUGIN_CANVAS_MENU_COMPONENT_BUTTON_ID = `${PLUGIN_CANVAS_MENU_COMPONENT_ID}.button`; | ||
export const PLUGIN_FEATURE_ID = `${PLUGIN_ID}`; | ||
export const PLUGIN_I18N_ID = `plugin.${PLUGIN_ID}.vectorize` | ||
export const PLUGIN_I18N_TRANSLATIONS = { | ||
en: { [PLUGIN_I18N_ID]: 'Vectorize' }, | ||
de: { [PLUGIN_I18N_ID]: 'Vektorisieren' } | ||
} | ||
export const ICON = '@imgly/icons/Vectorize' | ||
export const PLUGIN_ICON = '@imgly/icons/Vectorize' |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.