Skip to content

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Dec 13, 2024
1 parent b5439e0 commit 95927d7
Show file tree
Hide file tree
Showing 8 changed files with 25,044 additions and 43,559 deletions.
68,392 changes: 24,932 additions & 43,460 deletions dist/ha-draw.js

Large diffs are not rendered by default.

32 changes: 18 additions & 14 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import "tldraw/tldraw.css";

import UpdateStates from "./utilities/updateStates.tsx";
import { MetaUiHelper } from "./components/metaUiHelper.tsx";
import { getSnapShotFromServer, saveSnapshotToServer } from "./utilities/canvasStore.ts";
import {
getSnapShotFromServer,
saveSnapshotToServer,
} from "./utilities/canvasStore.ts";

declare global {
namespace JSX {
Expand Down Expand Up @@ -44,12 +47,14 @@ function App({ cardName }: ReactCardProps) {
type="text/css"
href="/hacsfiles/ha-draw/ha-draw.css"
/>
<div style={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
marginBottom: "10px"
}}>
<div
style={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
marginBottom: "10px",
}}
>
<p style={{ margin: 0 }}>{cardName}</p>
<div>
<button
Expand All @@ -58,24 +63,23 @@ function App({ cardName }: ReactCardProps) {
>
Load
</button>
<button onClick={handleSave}>
Save
</button>
<button onClick={handleSave}>Save</button>
</div>
</div>
<div
style={{
display: "flex",
flexDirection: "column",
height: "85vh"
height: "85vh",
}}
>
<div style={{ flex: "1 1 auto", overflow: "hidden" }}>
<Tldraw persistenceKey="persitenc-im-universum"
<Tldraw
persistenceKey="persitenc-im-universum"
onMount={(editor) => {
editorRef.current = editor;
}
}>
}}
>
<UpdateStates cardName={cardName} />
<MetaUiHelper />
</Tldraw>
Expand Down
4 changes: 1 addition & 3 deletions src/api/FileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export default class FileService {
}

async getSnapShot(): Promise<string> {

const response = await fetch(this.baseUrl, {
method: "GET",
headers: {
Expand All @@ -30,12 +29,11 @@ export default class FileService {
const form = new FormData();
form.append("jsondata", JSON.stringify(data));


const response = await fetch(this.baseUrl, {
method: "Post",
headers: {
Authorization: `Bearer ${this.token}`,
"ContentType": "multipart/form-data",
ContentType: "multipart/form-data",
},
body: form,
});
Expand Down
1 change: 0 additions & 1 deletion src/api/TemplateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default class TemplateService {
constructor(baseUrl: string, token: string) {
this.baseUrl = baseUrl + "/api/template"; //the URL of the HA Server
this.token = token; // The auth bearer Token from HA

}

async resolveTemplate(template: string): Promise<string> {
Expand Down
4 changes: 2 additions & 2 deletions src/components/metaUiHelper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export const MetaUiHelper = track(function MetaUiHelper() {
top: 64,
left: 12,
margin: 0,
userSelect: 'text',
pointerEvents: 'auto'
userSelect: "text",
pointerEvents: "auto",
}}
//For some Reason Coping doesn't work sometimes
onCopy={(e) => {
Expand Down
43 changes: 27 additions & 16 deletions src/utilities/canvasStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,51 @@ import FileService from "../api/FileService.ts";
import cardStates from "../cardStates.ts";
import { CardState } from "../types/hass.ts";

export async function saveSnapshotToServer(editor: Editor, cardName: string): Promise<void> {
export async function saveSnapshotToServer(
editor: Editor,
cardName: string,
): Promise<void> {
const cardState = cardStates.value[cardName] as CardState;
if (!cardState?.hass?.value) {
console.error("Missing hass or config for card:", cardName);
return;
}
const { hass } = cardState;

const { document } = getSnapshot(editor.store)
const fileService = new FileService(hass.value.auth.data.hassUrl, hass.value.auth.data.access_token);
try{
fileService.sendSnapShot(document)
} catch(err) {

const { document } = getSnapshot(editor.store);
const fileService = new FileService(
hass.value.auth.data.hassUrl,
hass.value.auth.data.access_token,
);
try {
fileService.sendSnapShot(document);
} catch (err) {
console.error(err);
}
}

export async function getSnapShotFromServer(editor: Editor, cardName: string): Promise<void> {
export async function getSnapShotFromServer(
editor: Editor,
cardName: string,
): Promise<void> {
const cardState = cardStates.value[cardName] as CardState;
if (!cardState?.hass?.value) {
console.error("Missing hass or config for card:", cardName);
return;
}
const { hass } = cardState;

const fileService = new FileService(hass.value.auth.data.hassUrl, hass.value.auth.data.access_token);
const fileService = new FileService(
hass.value.auth.data.hassUrl,
hass.value.auth.data.access_token,
);
try {
const jsonData: string = await fileService.getSnapShot()
const document = JSON.parse(jsonData)
editor.setCurrentTool('select')
loadSnapshot(editor.store, { document })
const jsonData: string = await fileService.getSnapShot();
const document = JSON.parse(jsonData);
editor.setCurrentTool("select");
loadSnapshot(editor.store, { document });
} catch (err) {
console.error(err);
return
return;
}

}
}
115 changes: 59 additions & 56 deletions src/utilities/drawBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export default function DrawBox(editor: Editor, group: GroupConfig): null {
}

let templateResult: any = group.template;
if(Colors.indexOf(templateResult) < 0 && group.tldraw.parameter == "color") {
templateResult = "red"
if (
Colors.indexOf(templateResult) < 0 &&
group.tldraw.parameter == "color"
) {
templateResult = "red";
}

//Boxes now have to be created manually
Expand All @@ -45,8 +48,6 @@ export default function DrawBox(editor: Editor, group: GroupConfig): null {
]);
}



if (group.tldraw.valuetype === "absolute") {
const num = Number(group.tldraw.lastvalue);
const current = Number(templateResult);
Expand All @@ -70,27 +71,27 @@ export default function DrawBox(editor: Editor, group: GroupConfig): null {
id: id,
props: {},
meta: {},
}
};

const params = group.tldraw.parameter.split(".");
if(params[1]){
switch (params[1]){
case "isClosed":
case "isComplete":
case "isPen":
templateResult = parseOrDefault(templateResult, "boolean");
break;
case "size":
templateResult = parseOrDefault(templateResult, "number");
break;
if (params[1]) {
switch (params[1]) {
case "isClosed":
case "isComplete":
case "isPen":
templateResult = parseOrDefault(templateResult, "boolean");
break;
case "size":
templateResult = parseOrDefault(templateResult, "number");
break;
}
} else {
switch (params[0]){
case "isLocked":
templateResult = parseOrDefault(templateResult, "boolean");
break;
case "rotation":
templateResult = parseOrDefault(templateResult, "number");
switch (params[0]) {
case "isLocked":
templateResult = parseOrDefault(templateResult, "boolean");
break;
case "rotation":
templateResult = parseOrDefault(templateResult, "number");
}
}
if (params[1]) {
Expand All @@ -103,30 +104,26 @@ export default function DrawBox(editor: Editor, group: GroupConfig): null {
update[params[0]] = templateResult;
}
update["meta"] = {
lastvalue: templateResult
lastvalue: templateResult,
};

editor.updateShape(
update,
);
editor.updateShape(update);
//https://tldraw.dev/reference/tlschema/TLGeoShapeProps

} catch (e) {
console.error(e);
createErrorBox(editor, id, e.toString().substring(0, 50));
}


return null;
}

function createErrorBox(editor: Editor, id: string, error: string) {
const errorID: string = id + "Error"
const errorID: string = id + "Error";

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const existingShape = editor.getShape(errorID)
const existingShape = editor.getShape(errorID);

if (!existingShape) {
editor.createShapes([
{
Expand All @@ -144,42 +141,48 @@ function createErrorBox(editor: Editor, id: string, error: string) {

function parseOrDefault<T>(
value: unknown,
type: 'string' | 'number' | 'boolean',
type: "string" | "number" | "boolean",
): T {
// Base default values
const defaultValues = {
'string': '' as unknown as T,
'number': 0 as unknown as T,
'boolean': false as unknown as T,
string: "" as unknown as T,
number: 0 as unknown as T,
boolean: false as unknown as T,
};

// Direct type match
if (typeof value === type) return value as T;

// Special handling for different types
try {
switch(type) {
case 'string':
return (value !== null && value !== undefined)
? String(value) as T
: defaultValues['string'];

case 'number':
return !isNaN( Number(value)) ? Number(value) as T : defaultValues['number'];

case 'boolean':
if (typeof value === 'string') {
const lowercaseValue = value.toLowerCase();
return (lowercaseValue === 'true' ? true :
lowercaseValue === 'false' ? false :
defaultValues['boolean']) as T;
}
return Boolean(value) as T;

default:
throw new Error('Unsupported type');
switch (type) {
case "string":
return value !== null && value !== undefined
? (String(value) as T)
: defaultValues["string"];

case "number":
return !isNaN(Number(value))
? (Number(value) as T)
: defaultValues["number"];

case "boolean":
if (typeof value === "string") {
const lowercaseValue = value.toLowerCase();
return (
lowercaseValue === "true"
? true
: lowercaseValue === "false"
? false
: defaultValues["boolean"]
) as T;
}
return Boolean(value) as T;

default:
throw new Error("Unsupported type");
}
} catch {
return defaultValues[type as keyof typeof defaultValues];
}
}
}
Loading

0 comments on commit 95927d7

Please sign in to comment.