Skip to content

Commit

Permalink
add element style editing
Browse files Browse the repository at this point in the history
  • Loading branch information
garredow committed Apr 17, 2024
1 parent 53210c3 commit aedc909
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nothing-special/kaiware-lib",
"version": "0.5.0",
"version": "0.6.0",
"type": "module",
"author": {
"name": "Garrett Downs",
Expand Down
19 changes: 19 additions & 0 deletions src/lib/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export class Connection {
case MessageType.GetElementStyles:
this.handleGetElementStyles(message);
break;
case MessageType.SetElementStyles:
this.handleSetElementStyles(message);
break;
case MessageType.GetElementData:
this.handleGetElementData(message);
break;
Expand Down Expand Up @@ -159,6 +162,22 @@ export class Connection {
});
}

private handleSetElementStyles(
message: MessageWithId & { type: MessageType.SetElementStyles }
) {
const element = document.querySelectorAll('*')[message.data.index] as HTMLElement;

Object.entries(message.data.styles).forEach(([key, value]) => {
element.style.setProperty(key, value);
});

this.sendMessage({
requestId: message.requestId,
type: MessageType.SetElementStylesRes,
data: null
});
}

private handleGetElementData(message: MessageWithId & { type: MessageType.GetElementData }) {
console.log('Getting data for element at index: ', message.data.index);

Expand Down
42 changes: 42 additions & 0 deletions src/types/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ export const getElementStylesPayloadSchema = z.object({
});
export type GetElementStylesPayload = z.infer<typeof getElementStylesPayloadSchema>;

// SetElementStyles
export const setElementStylesPayloadSchema = z.object({
index: z.number(),
styles: z.record(z.string())
});
export type SetElementStylesPayload = z.infer<typeof setElementStylesPayloadSchema>;

// SetElementStylesRes
export const setElementStylesResPayloadSchema = z.null();
export type SetElementStylesResPayload = z.infer<typeof setElementStylesResPayloadSchema>;

// GetElementStylesRes
export const getElementStylesResPayloadSchema = z.object({
index: z.number(),
Expand All @@ -48,6 +59,17 @@ export const getElementDataResPayloadSchema = z.object({
});
export type GetElementDataResPayload = z.infer<typeof getElementDataResPayloadSchema>;

// SetElementData
export const setElementDataPayloadSchema = z.object({
index: z.number(),
data: z.record(z.string(), z.unknown())
});
export type SetElementDataPayload = z.infer<typeof setElementDataPayloadSchema>;

// SetElementDataRes
export const setElementDataResPayloadSchema = z.null();
export type SetElementDataResPayload = z.infer<typeof setElementDataResPayloadSchema>;

// GetStorage
export const getStoragePayloadSchema = z.object({
storageType: z.union([z.literal('local'), z.literal('session')])
Expand Down Expand Up @@ -121,6 +143,16 @@ export const messageSchema = z.discriminatedUnion('type', [
type: z.literal(MessageType.GetElementStylesRes),
data: getElementStylesResPayloadSchema
}),
z.object({
requestId: z.string(),
type: z.literal(MessageType.SetElementStyles),
data: setElementStylesPayloadSchema
}),
z.object({
requestId: z.string(),
type: z.literal(MessageType.SetElementStylesRes),
data: setElementStylesResPayloadSchema
}),
z.object({
requestId: z.string(),
type: z.literal(MessageType.GetElementData),
Expand All @@ -131,6 +163,16 @@ export const messageSchema = z.discriminatedUnion('type', [
type: z.literal(MessageType.GetElementDataRes),
data: getElementDataResPayloadSchema
}),
z.object({
requestId: z.string(),
type: z.literal(MessageType.SetElementData),
data: setElementDataPayloadSchema
}),
z.object({
requestId: z.string(),
type: z.literal(MessageType.SetElementDataRes),
data: setElementDataResPayloadSchema
}),
z.object({
requestId: z.string(),
type: z.literal(MessageType.GetStorage),
Expand Down

0 comments on commit aedc909

Please sign in to comment.