alinea
Version:
Headless git-based CMS
56 lines (54 loc) • 1.3 kB
JavaScript
import "../../chunks/chunk-NZLE2WMY.js";
// src/core/field/ListField.ts
import { Field } from "../Field.js";
import { createId } from "../Id.js";
import { Schema } from "../Schema.js";
import { ListShape } from "../shape/ListShape.js";
import { generateKeyBetween } from "../util/FractionalIndexing.js";
var ListField = class extends Field {
constructor(schema, shapes, meta) {
super({
shape: new ListShape(
meta.options.label,
shapes,
meta.options.initialValue,
meta.postProcess
),
referencedViews: Schema.referencedViews(schema),
...meta
});
}
};
var ListEditor = class {
constructor(rows = []) {
this.rows = rows;
}
insertAt(insertAt, type, row) {
const id = createId();
const before = insertAt - 1;
const after = before + 1;
const keyA = this.rows[before]?._index || null;
const keyB = this.rows[after]?._index || null;
this.rows.push({
_id: id,
_index: generateKeyBetween(keyA, keyB),
_type: type,
...row
});
return this;
}
add(type, row) {
return this.insertAt(this.rows.length, type, row);
}
removeAt(index) {
this.rows.splice(index, 1);
return this;
}
value() {
return this.rows;
}
};
export {
ListEditor,
ListField
};