mirror of
https://github.com/penpot/penpot-plugins.git
synced 2025-01-07 15:39:49 -05:00
feat: real data contrast
This commit is contained in:
parent
4d3853f067
commit
c9b21e47ab
4 changed files with 35 additions and 10 deletions
|
@ -4,6 +4,7 @@ import './app.element.css';
|
||||||
|
|
||||||
export class AppElement extends HTMLElement {
|
export class AppElement extends HTMLElement {
|
||||||
public static observedAttributes = [];
|
public static observedAttributes = [];
|
||||||
|
public page: any;
|
||||||
|
|
||||||
calculateContrast(firstColor: string, secondColor: string) {
|
calculateContrast(firstColor: string, secondColor: string) {
|
||||||
const luminosityFirstColor = this.getLuminosity(firstColor);
|
const luminosityFirstColor = this.getLuminosity(firstColor);
|
||||||
|
@ -138,20 +139,38 @@ export class AppElement extends HTMLElement {
|
||||||
window.addEventListener('message', (event) => {
|
window.addEventListener('message', (event) => {
|
||||||
if (event.data.type === 'selection') {
|
if (event.data.type === 'selection') {
|
||||||
if (event.data.content.length === 2) {
|
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 {
|
} else {
|
||||||
this.setColors(null, null);
|
this.setColors(null, null);
|
||||||
this.setResult('0');
|
this.setResult('0');
|
||||||
this.setA11yTags(0);
|
this.setA11yTags(0);
|
||||||
}
|
}
|
||||||
} else if (event.data.type === 'page') {
|
} else if (event.data.type === 'page') {
|
||||||
console.log('refrespage', event.data);
|
this.page = event.data.content;
|
||||||
} else if (event.data.type === 'init') {
|
} else if (event.data.type === 'init') {
|
||||||
this.setAttribute('data-theme', event.data.content.theme);
|
this.setAttribute('data-theme', event.data.content.theme);
|
||||||
|
this.page = event.data.content.page;
|
||||||
|
|
||||||
if (event.data.content.selection.length === 2) {
|
if (event.data.content.selection.length === 2) {
|
||||||
//TODO get real colors from selection
|
const obj0 =
|
||||||
this.calculateContrast('#d5d1d1', '#000410');
|
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') {
|
} else if (event.data.type === 'theme') {
|
||||||
this.setAttribute('data-theme', event.data.content);
|
this.setAttribute('data-theme', event.data.content);
|
||||||
|
|
|
@ -17,6 +17,7 @@ penpot.ui.onMessage<{ content: string }>((message) => {
|
||||||
content: {
|
content: {
|
||||||
name: pageState.name,
|
name: pageState.name,
|
||||||
pageId: pageState.id,
|
pageId: pageState.id,
|
||||||
|
page: pageState,
|
||||||
fileId: fileState.id,
|
fileId: fileState.id,
|
||||||
revn: fileState.revn,
|
revn: fileState.revn,
|
||||||
theme: penpot.getTheme(),
|
theme: penpot.getTheme(),
|
||||||
|
@ -33,3 +34,7 @@ penpot.on('selectionchange', (id) => {
|
||||||
penpot.on('themechange', (theme) => {
|
penpot.on('themechange', (theme) => {
|
||||||
penpot.ui.sendMessage({ type: 'theme', content: theme });
|
penpot.ui.sendMessage({ type: 'theme', content: theme });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
penpot.on('pagechange', (page) => {
|
||||||
|
penpot.ui.sendMessage({ type: 'page', content: page });
|
||||||
|
});
|
||||||
|
|
|
@ -121,7 +121,7 @@ function getValue(value: unknown, type: string): unknown {
|
||||||
: value;
|
: 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 state = cljs.core.deref(app.main.store.state);
|
||||||
const statePath = getPath(path);
|
const statePath = getPath(path);
|
||||||
const data = cljs.core.get_in(state, statePath);
|
const data = cljs.core.get_in(state, statePath);
|
||||||
|
@ -173,8 +173,6 @@ export function getPartialState(path: string, state: UknowCljs) {
|
||||||
: name;
|
: name;
|
||||||
|
|
||||||
addEntry(childPath, propName, repValue, isExpandable);
|
addEntry(childPath, propName, repValue, isExpandable);
|
||||||
|
|
||||||
return;
|
|
||||||
} else {
|
} else {
|
||||||
const valueType = getType(entry);
|
const valueType = getType(entry);
|
||||||
const repValue = getValue(entry, valueType);
|
const repValue = getValue(entry, valueType);
|
||||||
|
@ -182,8 +180,6 @@ export function getPartialState(path: string, state: UknowCljs) {
|
||||||
const childPath = path + PATH_SEPARATOR + getKeyName(index);
|
const childPath = path + PATH_SEPARATOR + getKeyName(index);
|
||||||
|
|
||||||
addEntry(childPath, index, repValue, isExpandable);
|
addEntry(childPath, index, repValue, isExpandable);
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if (type.startsWith('js:')) {
|
} else if (type.startsWith('js:')) {
|
||||||
|
|
|
@ -25,7 +25,12 @@ export function initialize(api: any) {
|
||||||
api.addListener('plugin-page', 'page', (page: any) => {
|
api.addListener('plugin-page', 'page', (page: any) => {
|
||||||
console.log('Page Changed:', page);
|
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) => {
|
api.addListener('plugin-file', 'file', (file: any) => {
|
||||||
|
|
Loading…
Reference in a new issue