0
Fork 0
mirror of https://github.com/penpot/penpot-plugins.git synced 2025-01-06 14:50:21 -05:00

feat(parser): add getSelectedUuids and selection interface

This commit is contained in:
María Valderrama 2024-03-05 12:04:55 +01:00
parent f5ceae87b4
commit 2138085d39
7 changed files with 102 additions and 10 deletions

View file

@ -1,24 +1,59 @@
# Parser
This library includes a `parse()` funtion and some parsed models like `ParsedFile` or `ParsedPage`.
This library exports `parse()` and `getSelectedUuids()` funtions and some models like `ParsedFile`, `ParsedPage` or `UnparsedSelection`.
The parse function cleans up and transforms a penpot object into a more typescript friendly object. It returns a `ParsedData` object that can be casted as `ParsedFile` or `ParsedPage`. Note that `ParsedData` is the parent interface and includes all `ParsedFile` and `ParsedPage` properties.
The `parse()` function cleans up and transforms a penpot object into a more typescript friendly object. It returns a `ParsedData` object that can be casted as `ParsedFile` or `ParsedPage`. Note that `ParsedData` is the parent interface and includes all `ParsedFile` and `ParsedPage` properties.
Most of the properties are optional and may or may not be present in your result, you should access them with care.
The `getSelectedUuids()` functions, given an `UnparsedSelection` object, returns the selected Uuids as an array of string.
## Use
Import the parse function and the desired models from plugins data parser.
Example:
Examples:
```
- `parse()`
```ts
import { parse, ParsedFile } from 'plugins-parser';
[...]
const parsedFile: ParsedFile = parse(file);
console.log(parsedFile.data.colors?.[0]?.color);
const color = parsedFile.data.colors?.[0];
/** color:
* {
* "path": "Background",
* "color": "#1c1b1f",
* "fileId": "f13ed095-e13f-808c-8002-2830d45911f7",
* "name": "On Background",
* "opacity": 1,
* "id": "136eece0-40ab-8002-8002-296771ace070"
* }
*/
```
- `getSelectedUuids()`
```ts
import { getSelectedUuids, UnparsedSelection } from 'plugins-parser';
[...]
const selection: UnparsedSelection = { [...] };
const ids: string[] = getSelectedUuids(selection); =>
/** ids:
* [
* "4fa12080-0d58-80a3-8002-3a2344356e7c",
* "d9a61226-8431-8080-8002-5aea35acc724"
* ]
*/
```

View file

@ -1,2 +1,2 @@
export { parse } from './lib/utils';
export { parse, getSelectedUuids } from './lib/utils';
export * from './lib/models';

View file

@ -2,3 +2,4 @@ export * from './parsed.model';
export * from './utils.model';
export * from './file.model';
export * from './page.model';
export * from './selection.model';

View file

@ -0,0 +1,47 @@
export interface UnparsedSelection extends CljValues {
linked_map: SelLinkedMap;
}
export interface SelLinkedMap extends CljValues {
head: SelUuid | null;
delegate_map: SelDelegateMap;
}
export interface SelDelegateMap extends CljValues, UnderscoreValues, NilValues {
meta: unknown;
cnt: number;
root: SelRoot | null;
}
export interface SelRoot extends CljValues {
edit: unknown;
bitmap: number;
arr: SelArr[];
}
export interface SelArr extends UnderscoreValues, CljValues {
uuid?: string;
value: unknown;
left?: SelUuid;
right?: SelUuid;
}
export interface SelUuid extends CljValues, UnderscoreValues {
uuid: string;
}
export interface CljValues {
cljs$lang$protocol_mask$partition0$: number;
cljs$lang$protocol_mask$partition1$: number;
}
export interface UnderscoreValues {
__meta?: null | number;
__extmap?: null | number;
__hash: number | number;
}
export interface NilValues {
nil_val: null | boolean;
has_nil_QMARK_: null | boolean;
}

View file

@ -2,3 +2,4 @@ export * from './object.util';
export * from './parse-arr.util';
export * from './parse.util';
export * from './parse-properties.util';
export * from './selected.util';

View file

@ -11,9 +11,7 @@ import {
* Recursively cleans an object from unnecesary properties
* and converts snake_case and kebab-case to camelCase
*/
export function cleanObject(
obj: unknown
): Record<string, unknown> | Record<string, unknown>[] {
export function cleanObject(obj: unknown): unknown {
if (Array.isArray(obj)) {
return obj
.filter((p) => p !== null)
@ -25,7 +23,7 @@ export function cleanObject(
return Object.keys(obj as Record<string, unknown>)
.filter(
(key) =>
!/^(\$|cljs\$|__hash|_hash|bitmap|meta|ns|fqn|cnt|shift|edit|has_nil_QMARK_|nil_val)/g.test(
!/^(\$|cljs\$|__hash|_hash|bitmap|meta|extmap|ns|fqn|cnt|shift|edit|has_nil_QMARK_|nil_val)/g.test(
key
)
)

View file

@ -0,0 +1,10 @@
import { UnparsedSelection } from '../models/selection.model';
/**
* Gets selected uuids from selection object
*/
export function getSelectedUuids(selection: UnparsedSelection): string[] {
const root = selection?.linked_map?.delegate_map?.root?.arr;
return (root?.filter((r) => r?.uuid).map((r) => r.uuid) as string[]) || [];
}