Something went wrong. Try again.
Fork of github.com/ccmdi/propsec
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651import { App, Modal, setIcon } from "obsidian";import { FieldType, SchemaField, isPrimitiveType } from "../types";import { getTypeDisplayName } from "../schema/extractor";import { clearFieldConstraints } from "../utils/schema";import { ComparisonOperator, getComparisonOperatorOptions } from "../operators";
/** * Base class for modals that edit a list of schema fields with expandable constraint overlays. * Provides all shared field editing UI including: * - Field card rendering (name, type, required/warn, expand/delete) * - Constraint rendering (string, number, array) * - Expand/collapse state management * - Smart overlay positioning */export abstract class FieldEditorModal extends Modal { protected fieldsContainer: HTMLElement | null = null; protected expandedFields: Set<number> = new Set(); protected activeConstraintsSection: HTMLElement | null = null; protected scrollHandler: (() => void) | null = null;
constructor(app: App) { super(app); }
// ========== Abstract Methods ==========
/** Get the fields array being edited */ protected abstract getFields(): SchemaField[];
/** Set the fields array (used for filtering empty fields on save) */ protected abstract setFields(fields: SchemaField[]): void;
/** Get available types for the type dropdown (primitives + custom types) */ protected abstract getAvailableTypes(): string[];
/** Called when a field is deleted */ protected abstract onFieldDeleted(index: number): void;
/** Called when a new field should be added */ protected abstract onAddField(): void;
// ========== Expand/Collapse Management ==========
protected removeConstraintsSection(): void { if (this.activeConstraintsSection) { this.activeConstraintsSection.remove(); this.activeConstraintsSection = null; } }
protected closeAllExpanded(): void { this.removeConstraintsSection(); for (const index of this.expandedFields) { const card = this.fieldsContainer?.children[index] as HTMLElement; if (card) { card.removeClass("expanded"); const btn = card.querySelector(".propsec-icon-btn:not(.propsec-delete-btn)"); if (btn) setIcon(btn as HTMLElement, "chevron-right"); } } this.expandedFields.clear(); }
protected collapseField(index: number, card: HTMLElement, expandBtn: HTMLElement): void { if (this.activeConstraintsSection) { this.activeConstraintsSection.addClass("collapsing"); this.activeConstraintsSection.addEventListener("animationend", () => { this.removeConstraintsSection(); this.expandedFields.delete(index); card.removeClass("expanded"); setIcon(expandBtn, "chevron-right"); }, { once: true }); } else { this.expandedFields.delete(index); card.removeClass("expanded"); setIcon(expandBtn, "chevron-right"); } }
protected expandField( index: number, card: HTMLElement, expandBtn: HTMLElement, showOverlayCallback: () => void ): void { // Close any other expanded field first if (this.expandedFields.size > 0) { this.removeConstraintsSection(); const prevIndex = Array.from(this.expandedFields)[0]; const prevCard = this.fieldsContainer?.children[prevIndex] as HTMLElement; if (prevCard) { prevCard.removeClass("expanded"); const prevBtn = prevCard.querySelector(".propsec-icon-btn:not(.propsec-delete-btn)"); if (prevBtn) setIcon(prevBtn as HTMLElement, "chevron-right"); } this.expandedFields.clear(); } // Expand this field this.expandedFields.add(index); card.addClass("expanded"); setIcon(expandBtn, "chevron-down"); showOverlayCallback(); }
// ========== Field Rendering ==========
protected renderFields(emptyMessage: string = "No fields defined."): void { if (!this.fieldsContainer) return;
this.removeConstraintsSection(); this.expandedFields.clear(); this.fieldsContainer.empty();
const fields = this.getFields(); if (fields.length === 0) { this.fieldsContainer.createEl("p", { text: emptyMessage, cls: "propsec-no-fields", }); return; }
fields.forEach((field, index) => { this.renderFieldCard(this.fieldsContainer!, field, index); }); }
protected renderFieldCard( container: HTMLElement, field: SchemaField, index: number, onNameInputCreated?: (input: HTMLInputElement) => void ): HTMLElement { const card = container.createDiv({ cls: "propsec-field-card", });
const mainRow = card.createDiv({ cls: "propsec-field-main-row", });
// Name input const nameInput = mainRow.createEl("input", { type: "text", cls: "propsec-field-name", }); nameInput.value = field.name; nameInput.placeholder = "Field name"; nameInput.addEventListener("input", (e) => { field.name = (e.target as HTMLInputElement).value; });
// Callback for property suggestions onNameInputCreated?.(nameInput);
// Type select const typeSelect = mainRow.createEl("select", { cls: "propsec-field-type", });
const availableTypes = this.getAvailableTypes(); for (const type of availableTypes) { const option = typeSelect.createEl("option", { value: type, text: getTypeDisplayName(type), }); if (type === field.type) { option.selected = true; } }
typeSelect.addEventListener("change", (e) => { field.type = (e.target as HTMLSelectElement).value; clearFieldConstraints(field); this.removeConstraintsSection(); this.expandedFields.delete(index); this.replaceFieldCard(card, field, index, onNameInputCreated); });
// Required checkbox const requiredLabel = mainRow.createEl("label", { cls: "propsec-required-label", }); const requiredCheckbox = requiredLabel.createEl("input", { type: "checkbox", }); requiredCheckbox.checked = field.required; requiredLabel.appendText(" Req");
// Warn checkbox const warnLabel = mainRow.createEl("label", { cls: "propsec-required-label", attr: { title: "Warn if missing (not an error)" }, }); const warnCheckbox = warnLabel.createEl("input", { type: "checkbox", }); warnCheckbox.checked = field.warn || false; warnLabel.appendText(" Warn");
// Unique checkbox const uniqueLabel = mainRow.createEl("label", { cls: "propsec-required-label", attr: { title: "Value must be unique across all files in schema" }, }); const uniqueCheckbox = uniqueLabel.createEl("input", { type: "checkbox", }); uniqueCheckbox.checked = field.unique || false; uniqueLabel.appendText(" Uniq");
// Mutual exclusion handlers for required/warn requiredCheckbox.addEventListener("change", (e) => { field.required = (e.target as HTMLInputElement).checked; if (field.required) { field.warn = false; warnCheckbox.checked = false; } }); warnCheckbox.addEventListener("change", (e) => { field.warn = (e.target as HTMLInputElement).checked; if (field.warn) { field.required = false; requiredCheckbox.checked = false; } }); uniqueCheckbox.addEventListener("change", (e) => { field.unique = (e.target as HTMLInputElement).checked; });
// Expand button const hasConstraints = this.typeSupportsConstraints(field.type); const expandBtn = mainRow.createEl("button", { cls: "propsec-icon-btn", attr: { title: hasConstraints ? "Configure constraints" : "No constraints for this type" }, }); setIcon(expandBtn, "chevron-right");
if (hasConstraints) { expandBtn.addEventListener("click", () => { if (this.expandedFields.has(index)) { this.collapseField(index, card, expandBtn); } else { this.expandField(index, card, expandBtn, () => { this.showConstraintsOverlay(card, field); }); } }); } else { expandBtn.addClass("propsec-icon-btn-disabled"); expandBtn.disabled = true; }
// Delete button const deleteBtn = mainRow.createEl("button", { cls: "propsec-icon-btn propsec-delete-btn", attr: { title: "Remove field" }, }); setIcon(deleteBtn, "x"); deleteBtn.addEventListener("click", () => { this.onFieldDeleted(index); this.expandedFields.delete(index); this.renderFields(); });
return card; }
protected replaceFieldCard( oldCard: HTMLElement, field: SchemaField, index: number, onNameInputCreated?: (input: HTMLInputElement) => void ): void { const temp = document.createElement("div"); const newCard = this.renderFieldCard(temp, field, index, onNameInputCreated); oldCard.replaceWith(newCard); }
protected typeSupportsConstraints(type: FieldType): boolean { return isPrimitiveType(type) && ["string", "number", "date", "array"].includes(type); }
// ========== Constraints Overlay ==========
protected showConstraintsOverlay(card: HTMLElement, field: SchemaField): void { const rect = card.getBoundingClientRect();
const section = this.containerEl.createDiv({ cls: "propsec-constraints-section", });
section.style.left = `${rect.left}px`; section.style.width = `${rect.width}px`;
this.renderConstraints(section, field); this.activeConstraintsSection = section;
// Position after render so we know the height, keep on screen requestAnimationFrame(() => { const sectionRect = section.getBoundingClientRect(); const viewportHeight = window.innerHeight; const spaceBelow = viewportHeight - rect.bottom - 8; const spaceAbove = rect.top - 8;
if (sectionRect.height <= spaceBelow) { section.style.top = `${rect.bottom + 4}px`; } else if (sectionRect.height <= spaceAbove) { section.style.top = `${rect.top - sectionRect.height - 4}px`; } else { // Not enough space either way, position at bottom of viewport section.style.top = `${Math.max(8, viewportHeight - sectionRect.height - 8)}px`; section.style.maxHeight = `${viewportHeight - 16}px`; section.addClass("propsec-constraints-overflow"); } }); }
/** Override this to add content before type-specific constraints (e.g., conditions) */ protected renderConstraints(container: HTMLElement, field: SchemaField): void { switch (field.type) { case "string": this.renderStringConstraints(container, field); break; case "number": this.renderNumberConstraints(container, field); break; case "date": this.renderDateConstraints(container, field); break; case "array": this.renderArrayConstraints(container, field); break; } }
protected renderStringConstraints(container: HTMLElement, field: SchemaField): void { if (!field.stringConstraints) { field.stringConstraints = {}; } const constraints = field.stringConstraints;
container.createEl("div", { text: "String constraints", cls: "propsec-constraints-title", });
const grid = container.createDiv({ cls: "propsec-constraints-grid", });
// Pattern const patternRow = grid.createDiv({ cls: "propsec-constraint-row" }); patternRow.createEl("label", { text: "Pattern (regex):" }); const patternInput = patternRow.createEl("input", { type: "text", placeholder: "e.g., ^[A-Z].*", }); patternInput.value = constraints.pattern || ""; patternInput.addEventListener("input", (e) => { const val = (e.target as HTMLInputElement).value; constraints.pattern = val || undefined; });
// Min length const minLenRow = grid.createDiv({ cls: "propsec-constraint-row" }); minLenRow.createEl("label", { text: "Min length:" }); const minLenInput = minLenRow.createEl("input", { type: "number", attr: { min: "0" }, }); minLenInput.value = constraints.minLength !== undefined ? String(constraints.minLength) : ""; minLenInput.addEventListener("input", (e) => { const val = (e.target as HTMLInputElement).value; constraints.minLength = val ? parseInt(val, 10) : undefined; });
// Max length const maxLenRow = grid.createDiv({ cls: "propsec-constraint-row" }); maxLenRow.createEl("label", { text: "Max length:" }); const maxLenInput = maxLenRow.createEl("input", { type: "number", attr: { min: "0" }, }); maxLenInput.value = constraints.maxLength !== undefined ? String(constraints.maxLength) : ""; maxLenInput.addEventListener("input", (e) => { const val = (e.target as HTMLInputElement).value; constraints.maxLength = val ? parseInt(val, 10) : undefined; });
// Cross-field constraint this.renderCrossFieldConstraint(container, field); }
protected renderNumberConstraints(container: HTMLElement, field: SchemaField): void { if (!field.numberConstraints) { field.numberConstraints = {}; } const constraints = field.numberConstraints;
container.createEl("div", { text: "Number constraints", cls: "propsec-constraints-title", });
const grid = container.createDiv({ cls: "propsec-constraints-grid", });
// Min const minRow = grid.createDiv({ cls: "propsec-constraint-row" }); minRow.createEl("label", { text: "Min value:" }); const minInput = minRow.createEl("input", { type: "number" }); minInput.value = constraints.min !== undefined ? String(constraints.min) : ""; minInput.addEventListener("input", (e) => { const val = (e.target as HTMLInputElement).value; constraints.min = val ? parseFloat(val) : undefined; });
// Max const maxRow = grid.createDiv({ cls: "propsec-constraint-row" }); maxRow.createEl("label", { text: "Max value:" }); const maxInput = maxRow.createEl("input", { type: "number" }); maxInput.value = constraints.max !== undefined ? String(constraints.max) : ""; maxInput.addEventListener("input", (e) => { const val = (e.target as HTMLInputElement).value; constraints.max = val ? parseFloat(val) : undefined; });
// Cross-field constraint this.renderCrossFieldConstraint(container, field); }
protected renderDateConstraints(container: HTMLElement, field: SchemaField): void { if (!field.dateConstraints) { field.dateConstraints = {}; } const constraints = field.dateConstraints;
container.createEl("div", { text: "Date constraints", cls: "propsec-constraints-title", });
const grid = container.createDiv({ cls: "propsec-constraints-grid", });
// Min date const minRow = grid.createDiv({ cls: "propsec-constraint-row" }); minRow.createEl("label", { text: "Min date:" }); const minInput = minRow.createEl("input", { type: "date" }); minInput.value = constraints.min || ""; minInput.addEventListener("input", (e) => { const val = (e.target as HTMLInputElement).value; constraints.min = val || undefined; });
// Max date const maxRow = grid.createDiv({ cls: "propsec-constraint-row" }); maxRow.createEl("label", { text: "Max date:" }); const maxInput = maxRow.createEl("input", { type: "date" }); maxInput.value = constraints.max || ""; maxInput.addEventListener("input", (e) => { const val = (e.target as HTMLInputElement).value; constraints.max = val || undefined; });
// Cross-field constraint this.renderCrossFieldConstraint(container, field); }
protected renderCrossFieldConstraint(container: HTMLElement, field: SchemaField): void { container.createEl("div", { text: "Compare to another field", cls: "propsec-constraints-title", });
const grid = container.createDiv({ cls: "propsec-constraints-grid", });
// Operator select const operatorRow = grid.createDiv({ cls: "propsec-constraint-row" }); operatorRow.createEl("label", { text: "Must be:" }); const operatorSelect = operatorRow.createEl("select");
// Add empty option const emptyOption = operatorSelect.createEl("option", { value: "", text: "(none)", }); if (!field.crossFieldConstraint) { emptyOption.selected = true; }
for (const op of getComparisonOperatorOptions()) { const option = operatorSelect.createEl("option", { value: op.value, text: op.label, }); if (field.crossFieldConstraint?.operator === op.value) { option.selected = true; } }
// Field name input const fieldRow = grid.createDiv({ cls: "propsec-constraint-row" }); fieldRow.createEl("label", { text: "Field:" }); const fieldInput = fieldRow.createEl("input", { type: "text", placeholder: "other field name", }); fieldInput.value = field.crossFieldConstraint?.field || "";
// Update handlers const updateConstraint = () => { const operator = operatorSelect.value as ComparisonOperator | ""; const fieldName = fieldInput.value.trim();
if (operator && fieldName) { field.crossFieldConstraint = { operator: operator, field: fieldName, }; } else { delete field.crossFieldConstraint; } };
operatorSelect.addEventListener("change", updateConstraint); fieldInput.addEventListener("input", updateConstraint); }
protected renderArrayConstraints(container: HTMLElement, field: SchemaField): void { if (!field.arrayConstraints) { field.arrayConstraints = {}; } const constraints = field.arrayConstraints;
container.createEl("div", { text: "Array configuration", cls: "propsec-constraints-title", });
const grid = container.createDiv({ cls: "propsec-constraints-grid", });
// Element type selector const elementTypeRow = grid.createDiv({ cls: "propsec-constraint-row" }); elementTypeRow.createEl("label", { text: "Element type:" }); const elementTypeSelect = elementTypeRow.createEl("select");
// Add "any" option const anyOption = elementTypeSelect.createEl("option", { value: "", text: "(any type)", }); if (!field.arrayElementType) { anyOption.selected = true; }
const availableTypes = this.getAvailableTypes(); for (const type of availableTypes) { const option = elementTypeSelect.createEl("option", { value: type, text: getTypeDisplayName(type), }); if (type === field.arrayElementType) { option.selected = true; } }
elementTypeSelect.addEventListener("change", (e) => { const value = (e.target as HTMLSelectElement).value; field.arrayElementType = value || undefined; });
// Min items const minRow = grid.createDiv({ cls: "propsec-constraint-row" }); minRow.createEl("label", { text: "Min items:" }); const minInput = minRow.createEl("input", { type: "number", attr: { min: "0" }, }); minInput.value = constraints.minItems !== undefined ? String(constraints.minItems) : ""; minInput.addEventListener("input", (e) => { const val = (e.target as HTMLInputElement).value; constraints.minItems = val ? parseInt(val, 10) : undefined; });
// Max items const maxRow = grid.createDiv({ cls: "propsec-constraint-row" }); maxRow.createEl("label", { text: "Max items:" }); const maxInput = maxRow.createEl("input", { type: "number", attr: { min: "0" }, }); maxInput.value = constraints.maxItems !== undefined ? String(constraints.maxItems) : ""; maxInput.addEventListener("input", (e) => { const val = (e.target as HTMLInputElement).value; constraints.maxItems = val ? parseInt(val, 10) : undefined; });
// Contains const containsRow = grid.createDiv({ cls: "propsec-constraint-row" }); containsRow.createEl("label", { text: "Contains:" }); const containsInput = containsRow.createEl("input", { type: "text", placeholder: "value1, value2", }); containsInput.value = constraints.contains?.join(", ") || ""; containsInput.addEventListener("input", (e) => { const val = (e.target as HTMLInputElement).value; if (val.trim()) { constraints.contains = val.split(",").map((v) => v.trim()).filter((v) => v); } else { constraints.contains = undefined; } });
// Unique items const uniqueRow = grid.createDiv({ cls: "propsec-constraint-row" }); uniqueRow.createEl("label", { text: "Unique items:" }); const uniqueInput = uniqueRow.createEl("input", { type: "checkbox" }); uniqueInput.checked = constraints.uniqueItems ?? false; uniqueInput.addEventListener("change", (e) => { constraints.uniqueItems = (e.target as HTMLInputElement).checked || undefined; }); }
// ========== Cleanup ==========
protected setupScrollHandler(): void { this.scrollHandler = () => { if (this.activeConstraintsSection && this.expandedFields.size > 0) { this.closeAllExpanded(); } }; this.containerEl.addEventListener("scroll", this.scrollHandler, true); }
protected cleanupScrollHandler(): void { if (this.scrollHandler) { this.containerEl.removeEventListener("scroll", this.scrollHandler, true); } this.removeConstraintsSection(); }}