Skip to content

Commit

Permalink
Merge pull request #19 from Sapiens-OSS/remap
Browse files Browse the repository at this point in the history
Remap
  • Loading branch information
DecDuck authored Jan 17, 2024
2 parents 0bc3be4 + df77780 commit 62f1af4
Show file tree
Hide file tree
Showing 15 changed files with 6,949 additions and 21 deletions.
3 changes: 2 additions & 1 deletion components/CreateConfigModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ import {
import { FolderPlusIcon } from "@heroicons/vue/24/outline";
import { type File, type FullyLoadedProject } from "~/scripts/project";
import { VersionController } from "~/scripts/versionController";
import { randomUUID } from "~/scripts/utils/randomNumber";
const props = defineProps(["modelValue", "schemaid"]);
const emits = defineEmits(["update:modelValue"]);
Expand All @@ -130,7 +131,7 @@ const configName = ref("");
function createConfig() {
if (!project.value) return;
const newFile: File = {
id: crypto.randomUUID().split("-").join("").substring(0, 12),
id: randomUUID().split("-").join("").substring(0, 12),
name: configName.value,
value: {},
};
Expand Down
3 changes: 2 additions & 1 deletion components/CreateProjectModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ import {
} from "@headlessui/vue";
import { FolderPlusIcon } from "@heroicons/vue/24/outline";
import { PROJECT_SOURCES } from "~/scripts/project";
import { randomUUID } from "~/scripts/utils/randomNumber";
const props = defineProps(["modelValue"]);
const emits = defineEmits(["update:modelValue"]);
Expand Down Expand Up @@ -225,7 +226,7 @@ async function createProject() {
router.push(`/projects/${id}/`);
} catch (e) {
notifications.value.push({
uuid: crypto.randomUUID(),
uuid: randomUUID(),
type: NotificationType.TEXT,
title: "An error occurred while creating project",
description: e.toString(),
Expand Down
3 changes: 1 addition & 2 deletions components/SchemaArray.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<template>
<div v-if="props.schema?.enum"></div>
<div v-else>
<div>
<div class="border-b border-zinc-700 pb-1">
<div
class="-ml-4 -mt-4 flex flex-wrap items-center justify-between sm:flex-nowrap"
Expand Down
3 changes: 2 additions & 1 deletion components/SchemaNumber.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
import { CheckIcon, ChevronUpDownIcon } from "@heroicons/vue/20/solid";
import { TrashIcon } from "@heroicons/vue/24/outline";
import calculateSchemaTitle from "~/scripts/utils/calculateSchemaTitle";
import { randomUUID } from "~/scripts/utils/randomNumber";
const props = defineProps<{
schema: any;
Expand Down Expand Up @@ -81,5 +82,5 @@ const bump = computed(() => {
return 1;
});
const componentID = crypto.randomUUID();
const componentID = randomUUID();
</script>
10 changes: 6 additions & 4 deletions components/SchemaSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import SchemaArray from "./SchemaArray.vue";
import SchemaString from "./SchemaString.vue";
import SchemaNumber from "./SchemaNumber.vue";
import SchemaBoolean from "./SchemaBoolean.vue";
import SchemaTable from "./SchemaTable.vue";
const props = defineProps<{
schema: Schema;
Expand All @@ -29,7 +30,8 @@ type SchemaEditor =
| typeof SchemaArray
| typeof SchemaString
| typeof SchemaNumber
| typeof SchemaBoolean;
| typeof SchemaBoolean
| typeof SchemaTable;
const model = computed({
get() {
Expand All @@ -40,15 +42,15 @@ const model = computed({
},
});
// Not sure for what type to use, so we disable type checking with 'any'
const objects: { [key: string]: any } = {
const objects: { [key: string]: SchemaEditor } = {
object: SchemaObject,
array: SchemaArray,
string: SchemaString,
number: SchemaNumber,
integer: SchemaNumber,
boolean: SchemaBoolean,
remap: SchemaTable,
table: SchemaTable,
};
const figureOutComponentType: (schema: any) => SchemaEditor | null = (
Expand Down
3 changes: 2 additions & 1 deletion components/SchemaString.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ import {
import { CheckIcon, ChevronUpDownIcon } from "@heroicons/vue/20/solid";
import { TrashIcon } from "@heroicons/vue/24/outline";
import calculateSchemaTitle from "~/scripts/utils/calculateSchemaTitle";
import { randomUUID } from "~/scripts/utils/randomNumber";
const props = defineProps<{
schema: any;
Expand All @@ -143,5 +144,5 @@ const model = computed({
model.value ??= props.schema.default ?? "";
const componentID = crypto.randomUUID();
const componentID = randomUUID();
</script>
129 changes: 129 additions & 0 deletions components/SchemaTable.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<template>
<div>
<div class="border-b border-zinc-700 pb-1">
<div
class="-ml-4 -mt-4 flex flex-wrap items-center justify-between sm:flex-nowrap"
>
<div class="ml-4 mt-4">
<h3 class="text-base font-semibold leading-6 text-zinc-100">
{{ calculateSchemaTitle(props.schema) }}
</h3>
<p v-if="schema.description" class="mt-1 text-sm text-zinc-400">
{{ schema.description }}
</p>
</div>
</div>
</div>
<div class="space-y-2 mt-1 pl-2 lg:pl-4">
<div
v-for="entry in entries"
:key="persistentKeyTable[entry[0]]"
class="flex flex-row gap-x-2 items-center text-zinc-200"
>
<input
@input="onKeyUpdate"
:value="entry[0]"
type="text"
ref="inputRefs"
:data-persistence-key="persistentKeyTable[entry[0]]"
class="block w-full rounded-md bg-zinc-800/40 border-0 py-1.5 text-zinc-100 shadow-sm ring-1 ring-inset ring-zinc-700 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-orange-600 sm:text-sm sm:leading-6"
/>
<ArrowRightIcon class="w-10 h-10" />
<input
type="text"
class="block w-full rounded-md bg-zinc-800/40 border-0 py-1.5 text-zinc-100 shadow-sm ring-1 ring-inset ring-zinc-700 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-orange-600 sm:text-sm sm:leading-6"
v-model="model[entry[0]]"
/>
</div>
<div class="flex flex-row gap-x-2 items-center text-zinc-200">
<input
v-model="newEntry"
ref="rootInput"
type="text"
placeholder="Start typing..."
class="block w-full rounded-md bg-zinc-800/40 border-0 py-1.5 text-zinc-100 shadow-sm ring-1 ring-inset ring-zinc-700 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-orange-600 sm:text-sm sm:leading-6"
/>
<ArrowRightIcon class="w-10 h-10" />
<input
type="text"
disabled="true"
class="block w-full rounded-md bg-zinc-900/40 border-0 py-1.5 text-zinc-100 shadow-sm ring-1 ring-inset ring-zinc-800 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-orange-600 sm:text-sm sm:leading-6"
/>
</div>
</div>
</div>
</template>

<script setup lang="ts">
import { ArrowRightIcon } from "@heroicons/vue/24/outline";
import calculateSchemaTitle from "~/scripts/utils/calculateSchemaTitle";
import { randomUUID } from "~/scripts/utils/randomNumber";
const props = defineProps<{
schema: any;
modelValue: any;
}>();
const emit = defineEmits(["update:modelValue"]);
const model = computed({
get() {
return props.modelValue;
},
set(value) {
emit("update:modelValue", value);
},
});
const newEntry = computed({
get() {
return "";
},
set(key) {
if (!key) return;
const persistenceID = persistentKeyTable.value[key] ?? randomUUID();
if (!model.value[key]) {
model.value[key] = undefined;
persistentKeyTable.value[key] = persistenceID;
}
nextTick(() => {
const nodes = inputRefs.value.filter(
(e) => e?.getAttribute("data-persistence-key") == persistenceID
);
if (nodes.length > 0) {
nodes[0].focus();
}
});
},
});
model.value ??= {};
const entries = computed(() =>
model.value ? Object.entries(model.value) : []
);
const persistentKeyTable = ref<{ [key: string]: string }>({});
const inputRefs = ref<Array<HTMLInputElement>>([]);
const rootInput = ref<HTMLInputElement>();
function onKeyUpdate(input: Event) {
// @ts-expect-error
const key = input.target?.value;
// @ts-expect-error
const oldKey = input.target?._value;
if (key) {
// Copy value to new location
delete Object.assign(model.value, { [key]: model.value[oldKey] })[oldKey];
// Copy persistence key
delete Object.assign(persistentKeyTable.value, {
[key]: persistentKeyTable.value[oldKey],
})[oldKey];
} else {
delete model.value[oldKey];
delete persistentKeyTable.value[oldKey];
nextTick(() => {
rootInput.value?.focus();
})
}
}
</script>
3 changes: 2 additions & 1 deletion components/UploadAssetModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ import {
} from "@headlessui/vue";
import { CheckIcon, FolderPlusIcon } from "@heroicons/vue/24/outline";
import { type Asset, type FullyLoadedProject } from "~/scripts/project";
import { randomUUID } from "~/scripts/utils/randomNumber";
const props = defineProps(["modelValue"]);
Expand Down Expand Up @@ -171,7 +172,7 @@ async function upload() {
fr.readAsArrayBuffer(file);
});
const asset: Asset = {
id: crypto.randomUUID(),
id: randomUUID(),
name: filename.value,
filename: selectedFile.value.split("\\").at(-1),
data: new Blob([ab], { type: file.type }),
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"devDependencies": {
"@nuxt/devtools": "^1.0.8",
"@types/uuid": "^9.0.7",
"autoprefixer": "^10.4.16",
"nuxt": "^3.9.1",
"postcss": "^8.4.33",
Expand All @@ -31,6 +32,7 @@
"@types/file-saver": "^2.0.7",
"file-saver": "^2.0.5",
"highlight.js": "^11.9.0",
"jszip": "^3.10.1"
"jszip": "^3.10.1",
"uuid": "^9.0.1"
}
}
11 changes: 6 additions & 5 deletions pages/projects/[projectid].vue
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ import { cleanSchemaName } from "~/scripts/utils/cleanSchemaName";
import { mapSchemaIcon } from "~/scripts/utils/mapSchemaIcon";
import { Autosaver } from "~/scripts/autosaver";
import { VersionController } from "~/scripts/versionController";
import { randomUUID } from "~/scripts/utils/randomNumber";
// Globals
const route = useRoute();
Expand All @@ -364,7 +365,7 @@ const assets = useState<Asset[] | undefined>("assets", () => undefined);
projectLoader.catch((e) => {
notifications.value.push({
type: NotificationType.TEXT,
uuid: crypto.randomUUID(),
uuid: randomUUID(),
title: "An error occurred while loading the project",
description: e,
});
Expand Down Expand Up @@ -410,7 +411,7 @@ const constructProjectPath = (path: string) =>
async function loadSchemas() {
if (project.value == null) return;
navigation.value = generateNavigation();
const notificationID = crypto.randomUUID().toString();
const notificationID = randomUUID().toString();
const notifications = useNotifications();
notifications.value.push({
uuid: notificationID,
Expand All @@ -432,7 +433,7 @@ async function loadSchemas() {
);
const pushErrorNotification = () =>
notifications.value.push({
uuid: crypto.randomUUID().toString(),
uuid: randomUUID().toString(),
type: NotificationType.TEXT,
title: "Failed to download & import schemas",
description:
Expand Down Expand Up @@ -462,7 +463,7 @@ async function loadSchemas() {
navigation.value = generateNavigation();
const successNotificationID = crypto.randomUUID().toString();
const successNotificationID = randomUUID().toString();
notifications.value.push({
uuid: successNotificationID,
type: NotificationType.TEXT,
Expand Down Expand Up @@ -569,7 +570,7 @@ function saveProject() {
.saveProject(project.value)
.catch((e) => {
notifications.value.push({
uuid: crypto.randomUUID(),
uuid: randomUUID(),
type: NotificationType.TEXT,
title: "Error saving project",
description: e,
Expand Down
4 changes: 3 additions & 1 deletion pages/projects/[projectid]/index.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<template></template>
<script setup>
navigateTo("././home");
const router = useRouter();
router.replace("././home");
</script>
5 changes: 3 additions & 2 deletions scripts/indexeddb/indexedDBStorageSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ProjectSourceColour,
} from "../project";
import { DEFAULT_SCHEMAS } from "../schemas";
import { randomUUID } from "../utils/randomNumber";

const db = window.indexedDB.open("swb", 1);
const waitDB = new Promise<void>((r, j) => {
Expand All @@ -19,7 +20,7 @@ const waitDB = new Promise<void>((r, j) => {
waitDB.catch(() => {
const notifications = useNotifications();
notifications.value.push({
uuid: crypto.randomUUID(),
uuid: randomUUID(),
type: NotificationType.TEXT,
title: "Error opening IndexedDB database",
description: "An error occurred opening the IndexedDB.",
Expand Down Expand Up @@ -126,7 +127,7 @@ export class IndexedDBStorageSource implements ProjectSource {
newProject(name: string) {
return new Promise<string>(async (resolve, reject) => {
const projectStore = await useProjectObjectStore("readwrite");
const projectID = crypto.randomUUID();
const projectID = randomUUID();
const project: FullyLoadedProject | { projectSource: string } = {
id: projectID.toString(),
name: name,
Expand Down
3 changes: 2 additions & 1 deletion scripts/localStorage/localStorageSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ProjectSourceColour,
} from "../project";
import { DEFAULT_SCHEMAS } from "../schemas";
import { randomUUID } from "../utils/randomNumber";
import {
createOrSetDocument,
deleteDocument,
Expand Down Expand Up @@ -41,7 +42,7 @@ export class LocalStorageSource implements ProjectSource {
.map((e) => e.value);
}
async newProject(name: string) {
const projectID = crypto.randomUUID();
const projectID = randomUUID();
const project: FullyLoadedProject = {
id: projectID.toString(),
name: name,
Expand Down
9 changes: 9 additions & 0 deletions scripts/utils/randomNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { v4 as uuidv4 } from "uuid";

export function randomUUID() {
if (crypto.randomUUID) {
return crypto.randomUUID();
} else {
return uuidv4();
}
}
Loading

0 comments on commit 62f1af4

Please sign in to comment.