Skip to content

Commit

Permalink
wip: simplification and generalization #2
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHauschildt committed Feb 11, 2024
1 parent 4f11aba commit 4cddfea
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 43 deletions.
1 change: 1 addition & 0 deletions packages/vectorizer/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ const vectorize = async (cesdk: CreativeEditorSDK, params: { blockId: number })
const origX = engine.block.getPositionX(blockId)
const origY = engine.block.getPositionY(blockId)
const origSelected = engine.block.isSelected(blockId)

switch (engine.block.getType(blockId)) {
case "//ly.img.ubq/page":
{
Expand Down
15 changes: 7 additions & 8 deletions packages/vectorizer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ export interface PluginConfiguration {
// uploader ?
}



export { Manifest };

export default (pluginConfiguration: PluginConfiguration = {}) => {
return {
id: PLUGIN_ID,
version: PLUGIN_VERSION,
initialize(engine: CreativeEngineWithPolyfills) {

},
initializeUserInterface({ cesdk }: { cesdk: CreativeEditorSDK }) {
const engine = cesdk.engine as CreativeEngineWithPolyfills;
polyfillEngineWithCommands(engine);
console.log("checking if engine has polyfill_commands", engine.polyfill_commands? "yes": "no")
engine.event.subscribe([], async (events) => {
Expand All @@ -45,10 +47,7 @@ export default (pluginConfiguration: PluginConfiguration = {}) => {
}
});
});
},
initializeUserInterface({ cesdk }: { cesdk: CreativeEditorSDK }) {
const engine = cesdk.engine as CreativeEngineWithPolyfills;
polyfillEngineWithCommands(engine);

console.log("checking if engine has polyfill_commands", engine.polyfill_commands? "yes": "no")

engine.event.subscribe([], async (events) => {
Expand Down Expand Up @@ -81,8 +80,6 @@ export default (pluginConfiguration: PluginConfiguration = {}) => {
cesdk.ui.unstable_registerComponent(componentId, component);
})



// Always prepend the registered component to the canvas menu order.
console.info("Changing canvas menu order")
cesdk.ui.unstable_setCanvasMenuOrder([
Expand All @@ -91,6 +88,8 @@ export default (pluginConfiguration: PluginConfiguration = {}) => {
]);
},


// maybe this should be just engint.event.onUpdate()
update() {

},
Expand Down
43 changes: 10 additions & 33 deletions packages/vectorizer/src/ui.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { CreativeEngine } from '@cesdk/cesdk-js';

import {
PLUGIN_CANVAS_MENU_COMPONENT_BUTTON_ID,
PLUGIN_COMPONENT_BUTTON_ID,
PLUGIN_CANVAS_MENU_COMPONENT_ID,
PLUGIN_ACTION_VECTORIZE_LABEL,
PLUGIN_ICON
Expand All @@ -18,45 +16,24 @@ const button = (params: any) => {
const builder = params.builder!

const selected = engine.block.findAllSelected();
const isAnyBlockSupported = selected
.reduce((val, acc) => val || isBlockSupported(engine, acc), false)
if (!isAnyBlockSupported) return;

const actions: Array<() => void> = []

let anyIsLoading = false

let allCurrentProgress = 0
let allTotalProgress = 1
const candidates = selected.filter(id => isBlockSupported(engine, id))
if (candidates.length === 0) return;


for (const id of selected) {
if (!engine.block.hasFill(id)) return;
let isLoading = candidates.some(id => {
const metadata = getPluginMetadata(engine, id);
const isLoading = metadata.status === 'PROCESSING';
anyIsLoading ||= isLoading;
if (isLoading && metadata.progress) {
const { current, total } = metadata.progress;
allTotalProgress += total
allCurrentProgress += current
}

actions.push(() => engine.polyfill_commands?.executeCommand(PLUGIN_ACTION_VECTORIZE_LABEL, { blockId: id }))
}

return metadata.status === 'PROCESSING'
})


const loadingProgress = undefined
// console.log((allCurrentProgress / allTotalProgress) * 100)

builder.Button(PLUGIN_CANVAS_MENU_COMPONENT_BUTTON_ID, {
builder.Button(PLUGIN_COMPONENT_BUTTON_ID, {
label: PLUGIN_ACTION_VECTORIZE_LABEL,
icon: PLUGIN_ICON,
isActive: false,
isLoading: anyIsLoading,
isDisabled: anyIsLoading,
isLoading: isLoading,
isDisabled: isLoading,
loadingProgress,
onClick: () => actions.map(action => action())
onClick: () => candidates.forEach(id => engine.polyfill_commands?.executeCommand(PLUGIN_ACTION_VECTORIZE_LABEL, { blockId: id }))
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vectorizer/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const PLUGIN_ID = '@imgly/plugin-vectorizer-web';
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_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_ICON = '@imgly/icons/Vectorize'
Expand Down
2 changes: 1 addition & 1 deletion packages/vectorizer/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import {
export const isBlockSupported = (engine: CreativeEngine, blockId: number) => {
const blockType = engine.block.getType(blockId);
if (blockType === "//ly.img.ubq/page") return false; // There is some bug with the page block
return true;


if (engine.block.hasFill(blockId)) {
const fillId = engine.block.getFill(blockId);
const fillType = engine.block.getType(fillId);
Expand Down

0 comments on commit 4cddfea

Please sign in to comment.