UNPKG

@tldraw/tlschema

Version:

tldraw infinite canvas SDK (schema).

233 lines (232 loc) • 8.29 kB
"use strict"; 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 TLComment_exports = {}; __export(TLComment_exports, { commentReactionRecordConfig: () => commentReactionRecordConfig, commentRecordConfig: () => commentRecordConfig, commentSchemaRecords: () => commentSchemaRecords, commentThreadRecordConfig: () => commentThreadRecordConfig, createComment: () => createComment, createCommentId: () => createCommentId, createCommentReaction: () => createCommentReaction, createCommentReactionId: () => createCommentReactionId, createCommentThread: () => createCommentThread, createCommentThreadId: () => createCommentThreadId, isCommentId: () => isCommentId, isCommentReactionId: () => isCommentReactionId, isCommentThreadId: () => isCommentThreadId }); module.exports = __toCommonJS(TLComment_exports); var import_store = require("@tldraw/store"); var import_validate = require("@tldraw/validate"); var import_id_validator = require("../misc/id-validator"); var import_TLRichText = require("../misc/TLRichText"); var import_TLCustomRecord = require("./TLCustomRecord"); const emojiValidator = import_validate.T.string.check((value) => { if (value.length === 0 || value.length > 64) { throw new import_validate.T.ValidationError(`Expected an emoji of 1-64 characters, got ${value.length}`); } }); const commentAnchorValidator = import_validate.T.union("type", { shape: import_validate.T.object({ type: import_validate.T.literal("shape"), shapeId: (0, import_id_validator.idValidator)("shape"), x: import_validate.T.number, y: import_validate.T.number, isPrecise: import_validate.T.boolean }), point: import_validate.T.object({ type: import_validate.T.literal("point"), x: import_validate.T.number, y: import_validate.T.number }), region: import_validate.T.object({ type: import_validate.T.literal("region"), x: import_validate.T.number, y: import_validate.T.number, w: import_validate.T.number, h: import_validate.T.number, pinX: import_validate.T.number.optional(), pinY: import_validate.T.number.optional() }), page: import_validate.T.object({ type: import_validate.T.literal("page") }) }); function createCommentGuardMigrations(typeName, extra = []) { return (0, import_store.createRecordMigrationSequence)({ sequenceId: `com.tldraw.${typeName}`, recordType: typeName, retroactive: true, sequence: [ { id: `com.tldraw.${typeName}/1`, up: (record) => record }, ...extra ] }); } const commentThreadRecordConfig = { scope: "document", migrations: createCommentGuardMigrations("comment-thread", [ { // Shape anchors gained normalized x/y + isPrecise; existing ones were imprecise (top-right). id: "com.tldraw.comment-thread/2", up: (record) => { const anchor = record.anchor; if (anchor?.type === "shape" && anchor.x === void 0) { ; record.anchor = { ...anchor, x: 1, y: 0, isPrecise: false }; } return record; }, down: (record) => { const anchor = record.anchor; if (anchor?.type === "shape") { ; record.anchor = { type: "shape", shapeId: anchor.shapeId }; } return record; } }, { // Region anchors gained an optional pin corner (where the creating drag released). id: "com.tldraw.comment-thread/3", up: (record) => record, down: (record) => { const anchor = record.anchor; if (anchor?.type === "region") { const { pinX: _pinX, pinY: _pinY, ...rest } = anchor; record.anchor = rest; } return record; } } ]), validator: import_validate.T.object({ id: (0, import_id_validator.idValidator)("comment-thread"), typeName: import_validate.T.literal("comment-thread"), pageId: (0, import_id_validator.idValidator)("page"), anchor: commentAnchorValidator, createdBy: import_validate.T.string, createdAt: import_validate.T.number, resolved: import_validate.T.object({ at: import_validate.T.number, by: import_validate.T.string }).nullable(), isDeleted: import_validate.T.boolean, meta: import_validate.T.jsonValue }) }; const commentRecordConfig = { scope: "document", migrations: createCommentGuardMigrations("comment"), validator: import_validate.T.object({ id: (0, import_id_validator.idValidator)("comment"), typeName: import_validate.T.literal("comment"), threadId: (0, import_id_validator.idValidator)("comment-thread"), pageId: (0, import_id_validator.idValidator)("page"), authorId: import_validate.T.string, createdAt: import_validate.T.number, editedAt: import_validate.T.number.nullable(), body: import_TLRichText.richTextValidator, isDeleted: import_validate.T.boolean, meta: import_validate.T.jsonValue }) }; const commentReactionRecordConfig = { scope: "document", migrations: createCommentGuardMigrations("comment-reaction"), validator: import_validate.T.object({ id: (0, import_id_validator.idValidator)("comment-reaction"), typeName: import_validate.T.literal("comment-reaction"), commentId: (0, import_id_validator.idValidator)("comment"), threadId: (0, import_id_validator.idValidator)("comment-thread"), pageId: (0, import_id_validator.idValidator)("page"), userId: import_validate.T.string, emoji: emojiValidator, createdAt: import_validate.T.number, meta: import_validate.T.jsonValue }) }; const commentSchemaRecords = { "comment-thread": commentThreadRecordConfig, comment: commentRecordConfig, "comment-reaction": commentReactionRecordConfig }; function createCommentThreadId(id) { return (0, import_TLCustomRecord.createCustomRecordId)("comment-thread", id); } function createCommentId(id) { return (0, import_TLCustomRecord.createCustomRecordId)("comment", id); } function createCommentReactionId(commentId, userId, emoji) { return (0, import_TLCustomRecord.createCustomRecordId)( "comment-reaction", `${encodeURIComponent(commentId)}:${encodeURIComponent(userId)}:${encodeURIComponent(emoji)}` ); } function isCommentThreadId(id) { return (0, import_TLCustomRecord.isCustomRecordId)("comment-thread", id); } function isCommentId(id) { return (0, import_TLCustomRecord.isCustomRecordId)("comment", id); } function isCommentReactionId(id) { return (0, import_TLCustomRecord.isCustomRecordId)("comment-reaction", id); } function createCommentThread(props) { return { id: createCommentThreadId(), typeName: "comment-thread", pageId: props.pageId, anchor: props.anchor, createdBy: props.createdBy, createdAt: props.now ?? Date.now(), resolved: null, isDeleted: false, meta: props.meta ?? {} }; } function createComment(props) { return { id: createCommentId(), typeName: "comment", threadId: props.threadId, pageId: props.pageId, authorId: props.authorId, createdAt: props.now ?? Date.now(), editedAt: null, body: props.body, isDeleted: false, meta: props.meta ?? {} }; } function createCommentReaction(props) { return { id: createCommentReactionId(props.commentId, props.userId, props.emoji), typeName: "comment-reaction", commentId: props.commentId, threadId: props.threadId, pageId: props.pageId, userId: props.userId, emoji: props.emoji, createdAt: props.now ?? Date.now(), meta: props.meta ?? {} }; } //# sourceMappingURL=TLComment.js.map