@tldraw/tlschema
Version:
tldraw infinite canvas SDK (schema).
103 lines (102 loc) • 3.81 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var TLCustomRecord_exports = {};
__export(TLCustomRecord_exports, {
createCustomRecordId: () => createCustomRecordId,
createCustomRecordMigrationIds: () => createCustomRecordMigrationIds,
createCustomRecordMigrationSequence: () => createCustomRecordMigrationSequence,
createCustomRecordType: () => createCustomRecordType,
isCustomRecord: () => isCustomRecord,
isCustomRecordId: () => isCustomRecordId,
processCustomRecordMigrations: () => processCustomRecordMigrations
});
module.exports = __toCommonJS(TLCustomRecord_exports);
var import_store = require("@tldraw/store");
var import_utils = require("@tldraw/utils");
function createCustomRecordType(typeName, config) {
return (0, import_store.createRecordType)(typeName, {
scope: config.scope,
validator: config.validator
}).withDefaultProperties(config.createDefaultProperties ?? (() => ({})));
}
function processCustomRecordMigrations(records) {
const result = [];
for (const [typeName, config] of Object.entries(records)) {
const sequenceId = `com.tldraw.${typeName}`;
const { migrations } = config;
if (!migrations) {
result.push(
(0, import_store.createMigrationSequence)({
sequenceId,
retroactive: true,
sequence: []
})
);
} else if ("sequenceId" in migrations) {
(0, import_utils.assert)(
sequenceId === migrations.sequenceId,
`sequenceId mismatch for ${typeName} custom record migrations. Expected '${sequenceId}', got '${migrations.sequenceId}'`
);
result.push(migrations);
} else if ("sequence" in migrations) {
result.push(
(0, import_store.createMigrationSequence)({
sequenceId,
retroactive: true,
sequence: migrations.sequence.map((m) => {
if (!("id" in m)) return m;
return {
id: m.id,
dependsOn: m.dependsOn,
scope: "record",
filter: (r) => r.typeName === typeName,
up: (record) => {
const result2 = m.up(record);
if (result2) return result2;
},
down: typeof m.down === "function" ? (record) => {
const result2 = m.down(record);
if (result2) return result2;
} : void 0
};
})
})
);
}
}
return result;
}
function createCustomRecordMigrationIds(recordType, ids) {
return (0, import_utils.mapObjectMapValues)(ids, (_k, v) => `com.tldraw.${recordType}/${v}`);
}
function createCustomRecordMigrationSequence(migrations) {
return migrations;
}
function createCustomRecordId(typeName, id) {
return `${typeName}:${id ?? (0, import_utils.uniqueId)()}`;
}
function isCustomRecordId(typeName, id) {
if (!id) return false;
return id.startsWith(`${typeName}:`);
}
function isCustomRecord(typeName, record) {
if (!record) return false;
return record.typeName === typeName;
}
//# sourceMappingURL=TLCustomRecord.js.map