Skip to content

Commit

Permalink
fix: Types
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Jan 20, 2022
1 parent aee4460 commit 9fb7c4a
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"editor.formatOnSave": true
"editor.formatOnSave": true,
"cSpell.words": ["pako", "Serde", "serdes"]
}
4 changes: 2 additions & 2 deletions src/lib/components/actions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
const svgString = svg.outerHTML
.replaceAll('<br>', '<br/>')
.replaceAll(/<img([^>]*)>/g, (m, g) => `<img ${g} />`);
.replaceAll(/<img([^>]*)>/g, (m, g: string) => `<img ${g} />`);
return toBase64(svgString);
};
Expand Down Expand Up @@ -79,7 +79,7 @@
canvas.toBlob((blob) => {
try {
// @ts-ignore: https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1004/files
navigator.clipboard.write([
void navigator.clipboard.write([
/* eslint-disable no-undef */
// @ts-ignore: https://github.com/microsoft/TypeScript/issues/43821
new ClipboardItem({
Expand Down
6 changes: 4 additions & 2 deletions src/lib/util/env.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export const rendererUrl = import.meta.env.MERMAID_RENDERER_URL ?? 'https://mermaid.ink';
export const krokiRendererUrl = import.meta.env.MERMAID_KROKI_RENDERER_URL ?? 'https://kroki.io';
export const rendererUrl: string =
(import.meta.env.MERMAID_RENDERER_URL as string) ?? 'https://mermaid.ink';
export const krokiRendererUrl: string =
(import.meta.env.MERMAID_KROKI_RENDERER_URL as string) ?? 'https://kroki.io';
34 changes: 19 additions & 15 deletions src/lib/util/serde.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ interface Serde {
deserialize: (state: string) => string;
}

enum SerdeType {
Base64 = 'base64',
Pako = 'pako'
}

const base64Serde: Serde = {
serialize: (state: string): string => {
return toBase64(state, true);
Expand All @@ -32,26 +27,35 @@ export const pakoSerde: Serde = {
return inflate(data, { to: 'string' });
}
};
const serdes: Map<string, Serde> = new Map([
[SerdeType.Base64, base64Serde],
[SerdeType.Pako, pakoSerde]
]);

const serdes: { [key: string]: Serde } = {
base64: base64Serde,
pako: pakoSerde
};

type SerdeType = keyof typeof serdes;

export const serializeState = (state: State): string => {
const json = JSON.stringify(state);
const defaultSerde = SerdeType.Pako;
const serialized = serdes.get(defaultSerde).serialize(json);
const defaultSerde: SerdeType = 'pako';
const serialized = serdes[defaultSerde].serialize(json);
return `${defaultSerde}:${serialized}`;
};

export const deserializeState = (state: string): State => {
let type: string, serialized: string;
let type: SerdeType, serialized: string;
if (state.includes(':')) {
[type, serialized] = state.split(':');
let tempType: string;
[tempType, serialized] = state.split(':');
if (tempType in serdes) {
type = tempType;
} else {
throw new Error(`Unknown serde type: ${tempType}`);
}
} else {
type = SerdeType.Base64;
type = 'base64';
serialized = state;
}
const json = serdes.get(type).deserialize(serialized);
const json = serdes[type].deserialize(serialized);
return JSON.parse(json) as State;
};
2 changes: 1 addition & 1 deletion src/routes/edit.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
startLineNumber: e.hash.loc.first_line,
startColumn: e.hash.loc.first_column,
endLineNumber: e.hash.loc.last_line,
endColumn: e.hash.loc.last_column + 1,
endColumn: (e.hash.loc.last_column as number) + 1,
message: e.str
};
errorMarkers.push(marker);
Expand Down

0 comments on commit 9fb7c4a

Please sign in to comment.