-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from mindofmatthew/settings-editor-json-ts
Settings Editor Updates
- Loading branch information
Showing
12 changed files
with
356 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { autocompletion } from "@codemirror/autocomplete"; | ||
import { json } from "@codemirror/lang-json"; | ||
import { jsonSchema } from "codemirror-json-schema"; | ||
|
||
import { TidalSettingsSchema } from "packages/languages/tidal/settings"; | ||
|
||
export function settings() { | ||
return [ | ||
autocompletion(), | ||
json(), | ||
// TODO: Figure out how to get all the JSON Schema extensions to work together | ||
jsonSchema(TidalSettingsSchema), | ||
]; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { EventEmitter } from "@core/events"; | ||
|
||
import { getDefaults, getValid } from "./schema"; | ||
|
||
import { SettingsSchema, FromSchema } from "./schema"; | ||
|
||
interface StateEvents<S extends SettingsSchema> { | ||
change: FromSchema<S>; | ||
} | ||
|
||
export class StateManagement<S extends SettingsSchema> extends EventEmitter< | ||
StateEvents<S> | ||
> { | ||
private defaults: FromSchema<S>; | ||
private data: Partial<FromSchema<S>>; | ||
|
||
constructor(private schema: S, initial: any = {}) { | ||
super(); | ||
|
||
this.defaults = getDefaults(schema); | ||
this.data = getValid(schema, initial); | ||
} | ||
|
||
update(data: any) { | ||
this.data = getValid(this.schema, data); | ||
this.emit("change", this.getData()); | ||
} | ||
|
||
getData(): FromSchema<S> { | ||
return { ...this.defaults, ...this.data }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { JSONSchema, FromSchema as FromJSONSchema } from "json-schema-to-ts"; | ||
|
||
export interface SettingsSchema { | ||
properties: Readonly<Record<string, JSONSchema & object>>; | ||
} | ||
|
||
export type FromSchema<S extends SettingsSchema> = FromJSONSchema< | ||
S & { type: "object" } | ||
> & | ||
object; | ||
|
||
export function getDefaults< | ||
S extends SettingsSchema, | ||
SchemaData = FromSchema<S> | ||
>(schema: S): SchemaData { | ||
const defaults: any = {}; | ||
|
||
for (let key in schema.properties) { | ||
let valueOptions = schema.properties[key]; | ||
|
||
switch (valueOptions.type) { | ||
case "number": | ||
defaults[key] = valueOptions.default ?? 0; | ||
break; | ||
case "string": | ||
defaults[key] = valueOptions.default ?? ""; | ||
break; | ||
case "boolean": | ||
defaults[key] = valueOptions.default ?? false; | ||
break; | ||
case "array": | ||
defaults[key] = []; | ||
break; | ||
} | ||
} | ||
|
||
return defaults; | ||
} | ||
|
||
export function getValid<S extends SettingsSchema, SchemaData = FromSchema<S>>( | ||
schema: S, | ||
data: any | ||
): Partial<SchemaData> { | ||
const validData: any = {}; | ||
|
||
function getValidPrimitive(schema: JSONSchema & object, value: any) { | ||
if (schema.type === "number" && typeof value === "number") { | ||
return value; | ||
} else if (schema.type === "string" && typeof value === "string") { | ||
return value; | ||
} else if (schema.type === "boolean" && typeof value === "boolean") { | ||
return value; | ||
} | ||
} | ||
|
||
if (typeof data === "object") { | ||
for (let key in data) { | ||
if (key in schema.properties) { | ||
let prop = schema.properties[key]; | ||
if ( | ||
prop.type === "number" || | ||
prop.type === "string" || | ||
prop.type === "boolean" | ||
) { | ||
const value = getValidPrimitive(prop, data[key]); | ||
if (value !== undefined) { | ||
validData[key] = value; | ||
} | ||
} else if (prop.type === "array" && typeof prop.items === "object") { | ||
const value = data[key]; | ||
if (Array.isArray(value)) { | ||
const arraySchema = prop.items; | ||
validData[key] = value.filter((v) => | ||
getValidPrimitive(arraySchema, v) | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return validData; | ||
} |
Oops, something went wrong.