@drauu/core
Advanced tools
Comparing version 0.0.2 to 0.0.3
@@ -0,1 +1,5 @@ | ||
interface Unsubscribe { | ||
(): void | ||
} | ||
interface Brush { | ||
@@ -36,2 +40,5 @@ color: string; | ||
} | ||
interface EventsMap { | ||
update: () => void; | ||
} | ||
@@ -50,2 +57,3 @@ declare abstract class BaseModel<T extends SVGElement> { | ||
get shiftPressed(): boolean; | ||
get altPressed(): boolean; | ||
getMousePosition(event: MouseEvent | TouchEvent): { | ||
@@ -73,3 +81,2 @@ x: number; | ||
declare class Drauu { | ||
options: Options; | ||
el: SVGSVGElement | null; | ||
@@ -79,2 +86,4 @@ mode: DrawingMode; | ||
shiftPressed: boolean; | ||
altPressed: boolean; | ||
private _emitter; | ||
private _models; | ||
@@ -88,2 +97,3 @@ private _currentNode; | ||
unmounted(): void; | ||
on<K extends keyof EventsMap>(type: K, fn: EventsMap[K]): Unsubscribe; | ||
undo(): boolean; | ||
@@ -101,2 +111,2 @@ redo(): boolean; | ||
export { Brush, Drauu, DrawingMode, Options, Point, createDrauu }; | ||
export { Brush, Drauu, DrawingMode, EventsMap, Options, Point, createDrauu }; |
(() => { | ||
// ../../node_modules/.pnpm/nanoevents@6.0.0/node_modules/nanoevents/index.js | ||
var createNanoEvents = () => ({ | ||
events: {}, | ||
emit(event, ...args) { | ||
; | ||
(this.events[event] || []).forEach((i) => i(...args)); | ||
}, | ||
on(event, cb) { | ||
; | ||
(this.events[event] = this.events[event] || []).push(cb); | ||
return () => this.events[event] = (this.events[event] || []).filter((i) => i !== cb); | ||
} | ||
}); | ||
// src/utils.ts | ||
@@ -9,2 +23,10 @@ function decimal(d) { | ||
} | ||
function getSymbol(a) { | ||
if (a < 0) | ||
return -1; | ||
return 1; | ||
} | ||
function splitNum(a) { | ||
return [Math.abs(a), getSymbol(a)]; | ||
} | ||
@@ -35,2 +57,5 @@ // src/models/base.ts | ||
} | ||
get altPressed() { | ||
return this.drauu.altPressed; | ||
} | ||
getMousePosition(event) { | ||
@@ -161,4 +186,4 @@ const rect = this.drauu.el.getBoundingClientRect(); | ||
return false; | ||
let dx = Math.abs(point.x - this.start.x); | ||
let dy = Math.abs(point.y - this.start.y); | ||
let [dx, sx] = splitNum(point.x - this.start.x); | ||
let [dy, sy] = splitNum(point.y - this.start.y); | ||
if (this.shiftPressed) { | ||
@@ -169,4 +194,15 @@ const d = Math.min(dx, dy); | ||
} | ||
this.attr("rx", dx); | ||
this.attr("ry", dy); | ||
if (this.altPressed) { | ||
this.attr("cx", this.start.x); | ||
this.attr("cy", this.start.y); | ||
this.attr("rx", dx); | ||
this.attr("ry", dy); | ||
} else { | ||
const [x1, x2] = [this.start.x, this.start.x + dx * sx].sort(numSort); | ||
const [y1, y2] = [this.start.y, this.start.y + dy * sy].sort(numSort); | ||
this.attr("cx", (x1 + x2) / 2); | ||
this.attr("cy", (y1 + y2) / 2); | ||
this.attr("rx", (x2 - x1) / 2); | ||
this.attr("ry", (y2 - y1) / 2); | ||
} | ||
return true; | ||
@@ -214,4 +250,13 @@ } | ||
} | ||
this.attr("x2", x); | ||
this.attr("y2", y); | ||
if (this.altPressed) { | ||
this.attr("x1", this.start.x * 2 - x); | ||
this.attr("y1", this.start.y * 2 - y); | ||
this.attr("x2", x); | ||
this.attr("y2", y); | ||
} else { | ||
this.attr("x1", this.start.x); | ||
this.attr("y1", this.start.y); | ||
this.attr("x2", x); | ||
this.attr("y2", y); | ||
} | ||
return true; | ||
@@ -246,6 +291,4 @@ } | ||
return false; | ||
const [x1, x2] = [this.start.x, point.x].sort(numSort); | ||
const [y1, y2] = [this.start.y, point.y].sort(numSort); | ||
let dx = x2 - x1; | ||
let dy = y2 - y1; | ||
let [dx, sx] = splitNum(point.x - this.start.x); | ||
let [dy, sy] = splitNum(point.y - this.start.y); | ||
if (this.shiftPressed) { | ||
@@ -256,6 +299,15 @@ const d = Math.min(dx, dy); | ||
} | ||
this.attr("x", x1); | ||
this.attr("y", y1); | ||
this.attr("width", dx); | ||
this.attr("height", dy); | ||
if (this.altPressed) { | ||
this.attr("x", this.start.x - dx); | ||
this.attr("y", this.start.y - dy); | ||
this.attr("width", dx * 2); | ||
this.attr("height", dy * 2); | ||
} else { | ||
const [x1, x2] = [this.start.x, this.start.x + dx * sx].sort(numSort); | ||
const [y1, y2] = [this.start.y, this.start.y + dy * sy].sort(numSort); | ||
this.attr("x", x1); | ||
this.attr("y", y1); | ||
this.attr("width", x2 - x1); | ||
this.attr("height", y2 - y1); | ||
} | ||
return true; | ||
@@ -287,5 +339,6 @@ } | ||
constructor(options = {}) { | ||
this.options = options; | ||
this.el = null; | ||
this.shiftPressed = false; | ||
this.altPressed = false; | ||
this._emitter = createNanoEvents(); | ||
this._models = createModels(this); | ||
@@ -339,2 +392,5 @@ this._undoStack = []; | ||
} | ||
on(type, fn) { | ||
return this._emitter.on(type, fn); | ||
} | ||
undo() { | ||
@@ -358,2 +414,3 @@ const el = this.el; | ||
event.preventDefault(); | ||
this._emitter.emit("update"); | ||
} | ||
@@ -367,2 +424,3 @@ } | ||
this.el.appendChild(this._currentNode); | ||
this._emitter.emit("update"); | ||
} | ||
@@ -378,6 +436,9 @@ eventEnd(event) { | ||
} | ||
this._emitter.emit("update"); | ||
} | ||
eventKeyboard(event) { | ||
this.shiftPressed = event.shiftKey; | ||
this.altPressed = event.altKey; | ||
this.model.onMove(this.model.point); | ||
this._emitter.emit("update"); | ||
} | ||
@@ -384,0 +445,0 @@ commit() { |
@@ -15,2 +15,16 @@ var __defProp = Object.defineProperty; | ||
// ../../node_modules/.pnpm/nanoevents@6.0.0/node_modules/nanoevents/index.js | ||
var createNanoEvents = () => ({ | ||
events: {}, | ||
emit(event, ...args) { | ||
; | ||
(this.events[event] || []).forEach((i) => i(...args)); | ||
}, | ||
on(event, cb) { | ||
; | ||
(this.events[event] = this.events[event] || []).push(cb); | ||
return () => this.events[event] = (this.events[event] || []).filter((i) => i !== cb); | ||
} | ||
}); | ||
// src/utils.ts | ||
@@ -23,2 +37,10 @@ function decimal(d) { | ||
} | ||
function getSymbol(a) { | ||
if (a < 0) | ||
return -1; | ||
return 1; | ||
} | ||
function splitNum(a) { | ||
return [Math.abs(a), getSymbol(a)]; | ||
} | ||
@@ -49,2 +71,5 @@ // src/models/base.ts | ||
} | ||
get altPressed() { | ||
return this.drauu.altPressed; | ||
} | ||
getMousePosition(event) { | ||
@@ -175,4 +200,4 @@ const rect = this.drauu.el.getBoundingClientRect(); | ||
return false; | ||
let dx = Math.abs(point.x - this.start.x); | ||
let dy = Math.abs(point.y - this.start.y); | ||
let [dx, sx] = splitNum(point.x - this.start.x); | ||
let [dy, sy] = splitNum(point.y - this.start.y); | ||
if (this.shiftPressed) { | ||
@@ -183,4 +208,15 @@ const d = Math.min(dx, dy); | ||
} | ||
this.attr("rx", dx); | ||
this.attr("ry", dy); | ||
if (this.altPressed) { | ||
this.attr("cx", this.start.x); | ||
this.attr("cy", this.start.y); | ||
this.attr("rx", dx); | ||
this.attr("ry", dy); | ||
} else { | ||
const [x1, x2] = [this.start.x, this.start.x + dx * sx].sort(numSort); | ||
const [y1, y2] = [this.start.y, this.start.y + dy * sy].sort(numSort); | ||
this.attr("cx", (x1 + x2) / 2); | ||
this.attr("cy", (y1 + y2) / 2); | ||
this.attr("rx", (x2 - x1) / 2); | ||
this.attr("ry", (y2 - y1) / 2); | ||
} | ||
return true; | ||
@@ -228,4 +264,13 @@ } | ||
} | ||
this.attr("x2", x); | ||
this.attr("y2", y); | ||
if (this.altPressed) { | ||
this.attr("x1", this.start.x * 2 - x); | ||
this.attr("y1", this.start.y * 2 - y); | ||
this.attr("x2", x); | ||
this.attr("y2", y); | ||
} else { | ||
this.attr("x1", this.start.x); | ||
this.attr("y1", this.start.y); | ||
this.attr("x2", x); | ||
this.attr("y2", y); | ||
} | ||
return true; | ||
@@ -260,6 +305,4 @@ } | ||
return false; | ||
const [x1, x2] = [this.start.x, point.x].sort(numSort); | ||
const [y1, y2] = [this.start.y, point.y].sort(numSort); | ||
let dx = x2 - x1; | ||
let dy = y2 - y1; | ||
let [dx, sx] = splitNum(point.x - this.start.x); | ||
let [dy, sy] = splitNum(point.y - this.start.y); | ||
if (this.shiftPressed) { | ||
@@ -270,6 +313,15 @@ const d = Math.min(dx, dy); | ||
} | ||
this.attr("x", x1); | ||
this.attr("y", y1); | ||
this.attr("width", dx); | ||
this.attr("height", dy); | ||
if (this.altPressed) { | ||
this.attr("x", this.start.x - dx); | ||
this.attr("y", this.start.y - dy); | ||
this.attr("width", dx * 2); | ||
this.attr("height", dy * 2); | ||
} else { | ||
const [x1, x2] = [this.start.x, this.start.x + dx * sx].sort(numSort); | ||
const [y1, y2] = [this.start.y, this.start.y + dy * sy].sort(numSort); | ||
this.attr("x", x1); | ||
this.attr("y", y1); | ||
this.attr("width", x2 - x1); | ||
this.attr("height", y2 - y1); | ||
} | ||
return true; | ||
@@ -301,5 +353,6 @@ } | ||
constructor(options = {}) { | ||
this.options = options; | ||
this.el = null; | ||
this.shiftPressed = false; | ||
this.altPressed = false; | ||
this._emitter = createNanoEvents(); | ||
this._models = createModels(this); | ||
@@ -353,2 +406,5 @@ this._undoStack = []; | ||
} | ||
on(type, fn) { | ||
return this._emitter.on(type, fn); | ||
} | ||
undo() { | ||
@@ -372,2 +428,3 @@ const el = this.el; | ||
event.preventDefault(); | ||
this._emitter.emit("update"); | ||
} | ||
@@ -381,2 +438,3 @@ } | ||
this.el.appendChild(this._currentNode); | ||
this._emitter.emit("update"); | ||
} | ||
@@ -392,6 +450,9 @@ eventEnd(event) { | ||
} | ||
this._emitter.emit("update"); | ||
} | ||
eventKeyboard(event) { | ||
this.shiftPressed = event.shiftKey; | ||
this.altPressed = event.altKey; | ||
this.model.onMove(this.model.point); | ||
this._emitter.emit("update"); | ||
} | ||
@@ -398,0 +459,0 @@ commit() { |
{ | ||
"name": "@drauu/core", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"main": "dist/index.js", | ||
@@ -26,4 +26,4 @@ "module": "dist/index.mjs", | ||
"dev": "nr build --watch", | ||
"build": "tsup src/index.ts --format esm,cjs,iife --dts --no-splitting --clean" | ||
"build": "tsup src/index.ts --format esm,cjs,iife --dts --no-splitting --clean --dts-resolve" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41498
1451