Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vectorizer): Headless ready and move into worker for demo purposes #6

Open
wants to merge 32 commits into
base: feature/vectorize-plugin-ms
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1f17f48
feat(vectorizer): Simplify and move into worker
DanielHauschildt Feb 7, 2024
1a986b1
feat(vectorizer): Simplify and move into worker #2
DanielHauschildt Feb 7, 2024
cfff8f4
feat(vectorizer): Simplify and move into worker #3
DanielHauschildt Feb 7, 2024
af309e9
feat(vectorizer): Added support for multiselection
DanielHauschildt Feb 8, 2024
cf00db3
chore: Build working
DanielHauschildt Feb 9, 2024
b3a8791
fix(vectorizer): Updated lib to npm
DanielHauschildt Feb 9, 2024
8a828e9
fix(vectorizer): Updated lib to npm
DanielHauschildt Feb 9, 2024
941767e
chore: Added vercel headers
DanielHauschildt Feb 9, 2024
1ebb0a0
chore: Added vercel headers
DanielHauschildt Feb 9, 2024
4f11aba
wip: simplification and generalization
DanielHauschildt Feb 11, 2024
4cddfea
wip: simplification and generalization #2
DanielHauschildt Feb 11, 2024
5289b4a
wip: Polyfill experiements
DanielHauschildt Feb 12, 2024
e6639f8
fix: removed obsolte types
DanielHauschildt Feb 12, 2024
edf8783
chore: Added list of (potential) plugins
DanielHauschildt Feb 12, 2024
5f22833
feat: Polyfill Plugin
DanielHauschildt Feb 12, 2024
94645a7
feat: Polyfill Plugin
DanielHauschildt Feb 12, 2024
6855ffc
fix: Readded background removal
DanielHauschildt Feb 12, 2024
589a4da
fix: Build
DanielHauschildt Feb 12, 2024
6ba69d4
feat: Refactored app to make use of Commandpalette
DanielHauschildt Feb 12, 2024
12b5a49
wip: Added lot's of commands
DanielHauschildt Feb 13, 2024
52c52c5
chore: Refactored into plugin
DanielHauschildt Feb 13, 2024
6797250
feat: Polyfill I18N and more commands
DanielHauschildt Feb 14, 2024
13facde
wip
DanielHauschildt Feb 27, 2024
ab4c77c
wip
DanielHauschildt Feb 27, 2024
f15a1da
wip
DanielHauschildt Mar 1, 2024
34cfd3d
wip
DanielHauschildt Mar 1, 2024
38875f9
wip
DanielHauschildt Mar 1, 2024
06feae3
Onto Sync
DanielHauschildt Mar 2, 2024
8f50f7d
wip: Improved naming
DanielHauschildt Mar 3, 2024
4a8e47b
wip
DanielHauschildt Mar 3, 2024
1e806bd
feat: add plugin-documents
DanielHauschildt Mar 3, 2024
e2c2fe9
Wip
DanielHauschildt Mar 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .vscode/launch.json
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
Comment on lines +2 to +4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this comment? It looks wrong inside a json

Suggested change
// 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}"
}
]
}
9 changes: 5 additions & 4 deletions packages/vectorizer/esbuild/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ console.log(

const configs = [
{
entryPoints: ['src/index.ts'],
entryPoints: ['src/index.ts', "src/worker.ts"],
define: {
PLUGIN_VERSION: `"${packageJson.version}"`
},
minify: true,
bundle: true,
sourcemap: true,
external: ['@cesdk/cesdk-js', 'lodash', "node:path", "fs", "url"],
platform: 'browser',
external: ['@cesdk/cesdk-js'],
platform: 'node',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is node correct here? In the end the plugin will be shipped in the browser. Do we need two different configs and build for either nodejs or browser maybe?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I learned from the docs that "nodejs" is not the platform to build for, it has mechanism in place such das externalizing node modules and so forth. The platform itself has not influence on the end target.

format: 'esm',
outfile: 'dist/index.mjs',
outdir: 'dist',
outExtension: { '.js': '.mjs' },
plugins: [
{
name: 'reporter',
Expand Down
2 changes: 1 addition & 1 deletion packages/vectorizer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@cesdk/cesdk-js": "~1.20.0"
},
"dependencies": {
"@imgly/vectorizer": "^0.1.0-rc4",
"@imgly/vectorizer": "../vectorizer/packages/js",
"lodash": "^4.17.21"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type CreativeEditorSDK from '@cesdk/cesdk-js';
import { imageToSvg } from '@imgly/vectorizer';

import {
getPluginMetadata,
Expand All @@ -8,41 +7,30 @@ import {
setPluginMetadata
} from './utils';

class Scheduler<T> {
#queue?: Promise<T> = undefined

async schedule(task: () => Promise<T>): Promise<T> {
if (this.#queue === undefined) {
this.#queue = task()
} else {
this.#queue = this.#queue.then(async () => {
return await task()
})
}
return this.#queue
}
}

let scheduler = new Scheduler<any>()

import { PLUGIN_FEATURE_ID } from './constants';

import { runInWorker } from './worker.shared';
/**
* Apply the vectorization process to the image.
*/
async function vectorize(uri: string): Promise<Blob> {
const blob = await fetch(uri).then((res) => res.blob())
const outBlob = await scheduler.schedule(async () => await imageToSvg(blob));
return outBlob;
}

/**
* Triggers the vectiorize process.
*/
export async function processVectorization(
export async function vectorizeAction(
cesdk: CreativeEditorSDK,
blockId: number
params: { blockId: number }
) {
const blockApi = cesdk.engine.block;
const {blockId} = params;


const isFeatureEnabled = cesdk.feature.unstable_isEnabled(PLUGIN_FEATURE_ID, {
engine: cesdk.engine
})
if (!isFeatureEnabled) return;
const uploader = cesdk.unstable_upload.bind(cesdk)
const engine = cesdk.engine; // the only function that needs the ui is the upload function
const blockApi = engine.block;
if (!blockApi.hasFill(blockId))
throw new Error('Block has no fill to vectorize');

Expand Down Expand Up @@ -79,15 +67,14 @@ export async function processVectorization(

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(cesdk, blockId);
setPluginMetadata(cesdk, blockId, {
const metadata = getPluginMetadata(engine, blockId);
setPluginMetadata(engine, blockId, {
...metadata,
version: PLUGIN_VERSION,
initialSourceSet,
Expand All @@ -97,22 +84,19 @@ export async function processVectorization(
status: 'PROCESSING'
});

// Creating the mask from the highest resolution image
const vectorized = await vectorize(uriToProcess);
const vectorized: Blob = await runInWorker(uriToProcess)

// Check for externally changed state while we were uploading and
// do not proceed if the state was reset.
if (
getPluginMetadata(cesdk, blockId).status !== 'PROCESSING' ||
!isMetadataConsistent(cesdk, blockId)
getPluginMetadata(engine, blockId).status !== 'PROCESSING' ||
!isMetadataConsistent(engine, blockId)
)
return;

const pathname = new URL(uriToProcess).pathname;
const parts = pathname.split('/');
const filename = parts[parts.length - 1];

const uploadedAssets = await cesdk.unstable_upload(
const uploadedAssets = await uploader(
new File([vectorized], filename, { type: vectorized.type }),
() => {
// TODO Delegate process to UI component
Expand All @@ -122,8 +106,8 @@ export async function processVectorization(
// Check for externally changed state while we were uploading and
// do not proceed if the state was reset.
if (
getPluginMetadata(cesdk, blockId).status !== 'PROCESSING' ||
!isMetadataConsistent(cesdk, blockId)
getPluginMetadata(engine, blockId).status !== 'PROCESSING' ||
!isMetadataConsistent(engine, blockId)
)
return;

Expand All @@ -132,21 +116,21 @@ export async function processVectorization(
throw new Error('Could not upload vectorized image');
}

setPluginMetadata(cesdk, blockId, {
setPluginMetadata(engine, blockId, {
version: PLUGIN_VERSION,
initialSourceSet,
initialImageFileURI,
blockId,
fillId,
status: 'PROCESSED_TOGGLE_ON',
status: 'PROCESSED',
processedAsset: url
});
blockApi.setString(fillId, 'fill/image/imageFileURI', url);
// Finally, create an undo step
cesdk.engine.editor.addUndoStep();
engine.editor.addUndoStep();
} catch (error) {
if (cesdk.engine.block.isValid(blockId)) {
setPluginMetadata(cesdk, blockId, {
if (engine.block.isValid(blockId)) {
setPluginMetadata(engine, blockId, {
version: PLUGIN_VERSION,
initialSourceSet,
initialImageFileURI,
Expand All @@ -155,7 +139,7 @@ export async function processVectorization(
status: 'ERROR'
});

recoverInitialImageData(cesdk, blockId);
recoverInitialImageData(engine, blockId);
}
// eslint-disable-next-line no-console
console.error(error);
Expand Down
16 changes: 8 additions & 8 deletions packages/vectorizer/src/constants.ts
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_ACTION_VECTORIZE_LABEL = `plugin.${PLUGIN_ID}.vectorize`
export const PLUGIN_I18N_TRANSLATIONS = {
en: { [PLUGIN_ACTION_VECTORIZE_LABEL]: 'Vectorize' },
de: { [PLUGIN_ACTION_VECTORIZE_LABEL]: 'Vektorisieren' }
}
export const ICON = '@imgly/icons/Vectorize'
export const PLUGIN_ICON = '@imgly/icons/Vectorize'
28 changes: 0 additions & 28 deletions packages/vectorizer/src/enableFeatures.ts

This file was deleted.

Loading