From c9b21e47ab5b2627d1220e532f53d369f38e5986 Mon Sep 17 00:00:00 2001 From: Juanfran Date: Thu, 14 Mar 2024 13:53:01 +0100 Subject: [PATCH] feat: real data contrast --- apps/contrast-plugin/src/app/app.element.ts | 27 ++++++++++++++++--- apps/contrast-plugin/src/plugin.ts | 5 ++++ .../src/lib/utils/parse-state.ts | 6 +---- libs/plugins-runtime/src/index.ts | 7 ++++- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/apps/contrast-plugin/src/app/app.element.ts b/apps/contrast-plugin/src/app/app.element.ts index 00e0ceb..1b64fe5 100644 --- a/apps/contrast-plugin/src/app/app.element.ts +++ b/apps/contrast-plugin/src/app/app.element.ts @@ -4,6 +4,7 @@ import './app.element.css'; export class AppElement extends HTMLElement { public static observedAttributes = []; + public page: any; calculateContrast(firstColor: string, secondColor: string) { const luminosityFirstColor = this.getLuminosity(firstColor); @@ -138,20 +139,38 @@ export class AppElement extends HTMLElement { window.addEventListener('message', (event) => { if (event.data.type === 'selection') { if (event.data.content.length === 2) { - this.calculateContrast('#d5d1d1', '#000410'); + const obj0 = + this.page.objects['#' + event.data.content[0]]?.fills?.[0] + ?.fillColor; + const obj1 = + this.page.objects['#' + event.data.content[1]]?.fills?.[0] + ?.fillColor; + + if (obj0 && obj1) { + this.calculateContrast(obj0, obj1); + } } else { this.setColors(null, null); this.setResult('0'); this.setA11yTags(0); } } else if (event.data.type === 'page') { - console.log('refrespage', event.data); + this.page = event.data.content; } else if (event.data.type === 'init') { this.setAttribute('data-theme', event.data.content.theme); + this.page = event.data.content.page; if (event.data.content.selection.length === 2) { - //TODO get real colors from selection - this.calculateContrast('#d5d1d1', '#000410'); + const obj0 = + this.page.objects['#' + event.data.content.selection[0]]?.fills?.[0] + ?.fillColor; + const obj1 = + this.page.objects['#' + event.data.content.selection[1]]?.fills?.[0] + ?.fillColor; + + if (obj0 && obj1) { + this.calculateContrast(obj0, obj1); + } } } else if (event.data.type === 'theme') { this.setAttribute('data-theme', event.data.content); diff --git a/apps/contrast-plugin/src/plugin.ts b/apps/contrast-plugin/src/plugin.ts index 81b4612..09ff62d 100644 --- a/apps/contrast-plugin/src/plugin.ts +++ b/apps/contrast-plugin/src/plugin.ts @@ -17,6 +17,7 @@ penpot.ui.onMessage<{ content: string }>((message) => { content: { name: pageState.name, pageId: pageState.id, + page: pageState, fileId: fileState.id, revn: fileState.revn, theme: penpot.getTheme(), @@ -33,3 +34,7 @@ penpot.on('selectionchange', (id) => { penpot.on('themechange', (theme) => { penpot.ui.sendMessage({ type: 'theme', content: theme }); }); + +penpot.on('pagechange', (page) => { + penpot.ui.sendMessage({ type: 'page', content: page }); +}); diff --git a/libs/plugins-data-parser/src/lib/utils/parse-state.ts b/libs/plugins-data-parser/src/lib/utils/parse-state.ts index d429605..1a9e665 100644 --- a/libs/plugins-data-parser/src/lib/utils/parse-state.ts +++ b/libs/plugins-data-parser/src/lib/utils/parse-state.ts @@ -121,7 +121,7 @@ function getValue(value: unknown, type: string): unknown { : value; } -export function getPartialState(path: string, state: UknowCljs) { +export function getPartialState(path: string, state: UknowCljs): UknowCljs { // const state = cljs.core.deref(app.main.store.state); const statePath = getPath(path); const data = cljs.core.get_in(state, statePath); @@ -173,8 +173,6 @@ export function getPartialState(path: string, state: UknowCljs) { : name; addEntry(childPath, propName, repValue, isExpandable); - - return; } else { const valueType = getType(entry); const repValue = getValue(entry, valueType); @@ -182,8 +180,6 @@ export function getPartialState(path: string, state: UknowCljs) { const childPath = path + PATH_SEPARATOR + getKeyName(index); addEntry(childPath, index, repValue, isExpandable); - - return; } }); } else if (type.startsWith('js:')) { diff --git a/libs/plugins-runtime/src/index.ts b/libs/plugins-runtime/src/index.ts index ca7df6d..e27988b 100644 --- a/libs/plugins-runtime/src/index.ts +++ b/libs/plugins-runtime/src/index.ts @@ -25,7 +25,12 @@ export function initialize(api: any) { api.addListener('plugin-page', 'page', (page: any) => { console.log('Page Changed:', page); - setPageState(page); + const workspaceData = getPartialState( + 'root/:workspace-data', + api.getState() + ); + + setPageState(workspaceData.pagesIndex['#' + page.id]); }); api.addListener('plugin-file', 'file', (file: any) => {