UNPKG

@tldraw/editor

Version:

tldraw infinite canvas SDK (editor).

232 lines (231 loc) • 9.87 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name); var __typeError = (msg) => { throw TypeError(msg); }; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __decoratorStart = (base) => [, , , __create(base?.[__knownSymbol("metadata")] ?? null)]; var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"]; var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn; var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) }); var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]); var __runInitializers = (array, flags, self, value) => { for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value); return value; }; var __decorateElement = (array, flags, name, decorators, target, extra) => { var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16); var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5]; var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []); var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() { return __privateGet(this, extra); }, set [name](x) { return __privateSet(this, extra, x); } }, name)); k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name); for (var i = decorators.length - 1; i >= 0; i--) { ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers); if (k) { ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x }; if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name]; if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y; } it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1; if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it); else if (typeof it !== "object" || it === null) __typeError("Object expected"); else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn); } return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target; }; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg); var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj); var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj)); var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value); var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method); var _cancelDoubleClickTimeout_dec, __getClickTimeout_dec, _init; import { bind, uniqueId } from "@tldraw/utils"; import { Vec } from "../../../primitives/Vec.mjs"; const MAX_CLICK_DISTANCE = 40; __getClickTimeout_dec = [bind], _cancelDoubleClickTimeout_dec = [bind]; class ClickManager { constructor(editor) { this.editor = editor; __runInitializers(_init, 5, this); __publicField(this, "_clickId", ""); __publicField(this, "_clickTimeout"); __publicField(this, "_clickScreenPoint"); __publicField(this, "_previousScreenPoint"); /** * The current click state. * * @internal */ __publicField(this, "_clickState", "idle"); __publicField(this, "lastPointerInfo", {}); } _getClickTimeout(state, id = uniqueId()) { this._clickId = id; clearTimeout(this._clickTimeout); this._clickTimeout = this.editor.timers.setTimeout( () => { if (this._clickState === state && this._clickId === id) { switch (this._clickState) { case "pendingTriple": { this.editor.dispatch({ ...this.lastPointerInfo, type: "click", name: "double_click", phase: "settle" }); break; } case "pendingQuadruple": { this.editor.dispatch({ ...this.lastPointerInfo, type: "click", name: "triple_click", phase: "settle" }); break; } case "pendingOverflow": { this.editor.dispatch({ ...this.lastPointerInfo, type: "click", name: "quadruple_click", phase: "settle" }); break; } default: { } } this._clickState = "idle"; } }, state === "idle" || state === "pendingDouble" ? this.editor.options.doubleClickDurationMs : this.editor.options.multiClickDurationMs ); } /** * The current click state. * * @public */ // eslint-disable-next-line no-restricted-syntax get clickState() { return this._clickState; } handlePointerEvent(info) { switch (info.name) { case "pointer_down": { if (!this._clickState) return info; this._clickScreenPoint = Vec.From(info.point); if (this._previousScreenPoint && Vec.Dist2(this._previousScreenPoint, this._clickScreenPoint) > MAX_CLICK_DISTANCE ** 2) { this._clickState = "idle"; } this._previousScreenPoint = this._clickScreenPoint; this.lastPointerInfo = info; switch (this._clickState) { case "pendingDouble": { this._clickState = "pendingTriple"; this._clickTimeout = this._getClickTimeout(this._clickState); return { ...info, type: "click", name: "double_click", phase: "down" }; } case "pendingTriple": { this._clickState = "pendingQuadruple"; this._clickTimeout = this._getClickTimeout(this._clickState); return { ...info, type: "click", name: "triple_click", phase: "down" }; } case "pendingQuadruple": { this._clickState = "pendingOverflow"; this._clickTimeout = this._getClickTimeout(this._clickState); return { ...info, type: "click", name: "quadruple_click", phase: "down" }; } case "idle": { this._clickState = "pendingDouble"; break; } case "pendingOverflow": { this._clickState = "overflow"; break; } default: { } } this._clickTimeout = this._getClickTimeout(this._clickState); return info; } case "pointer_up": { if (!this._clickState) return info; this._clickScreenPoint = Vec.From(info.point); switch (this._clickState) { case "pendingTriple": { return { ...this.lastPointerInfo, type: "click", name: "double_click", phase: "up" }; } case "pendingQuadruple": { return { ...this.lastPointerInfo, type: "click", name: "triple_click", phase: "up" }; } case "pendingOverflow": { return { ...this.lastPointerInfo, type: "click", name: "quadruple_click", phase: "up" }; } default: { } } return info; } case "pointer_move": { if (this._clickState !== "idle" && this._clickScreenPoint && Vec.Dist2(this._clickScreenPoint, this.editor.inputs.currentScreenPoint) > (this.editor.getInstanceState().isCoarsePointer ? this.editor.options.coarseDragDistanceSquared : this.editor.options.dragDistanceSquared)) { this.cancelDoubleClickTimeout(); } return info; } } return info; } cancelDoubleClickTimeout() { this._clickTimeout = clearTimeout(this._clickTimeout); this._clickState = "idle"; } } _init = __decoratorStart(null); __decorateElement(_init, 1, "_getClickTimeout", __getClickTimeout_dec, ClickManager); __decorateElement(_init, 1, "cancelDoubleClickTimeout", _cancelDoubleClickTimeout_dec, ClickManager); __decoratorMetadata(_init, ClickManager); export { ClickManager }; //# sourceMappingURL=ClickManager.mjs.map