sticky-horse
Version:
With StickyHorse allow your users to send feedback to your team.
234 lines (233 loc) • 13.9 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DraggableNote = void 0;
var react_1 = __importStar(require("react"));
var react_toastify_1 = require("react-toastify");
require("react-toastify/dist/ReactToastify.css");
exports.DraggableNote = (0, react_1.forwardRef)(function (_a, ref) {
var id = _a.id, initialPosition = _a.initialPosition, _b = _a.initialContent, initialContent = _b === void 0 ? '' : _b, _c = _a.color, color = _c === void 0 ? 'bg-yellow-100' : _c, onPositionChange = _a.onPositionChange, onContentChange = _a.onContentChange, onClose = _a.onClose, onSendEmail = _a.onSendEmail, _d = _a.autoFocus, autoFocus = _d === void 0 ? false : _d, toEmail = _a.toEmail, _e = _a.isFollowingMouse, initialIsFollowingMouse = _e === void 0 ? true : _e, isDraggingNew = _a.isDraggingNew, setIsDraggingNew = _a.setIsDraggingNew;
var _f = (0, react_1.useState)(initialPosition || { x: 0, y: 0 }), position = _f[0], setPosition = _f[1];
var _g = (0, react_1.useState)(initialContent), content = _g[0], setContent = _g[1];
var _h = (0, react_1.useState)(false), isDragging = _h[0], setIsDragging = _h[1];
var _j = (0, react_1.useState)(initialIsFollowingMouse), isFollowingMouse = _j[0], setIsFollowingMouse = _j[1];
var _k = (0, react_1.useState)({ x: 150, y: 100 }), dragOffset = _k[0], setDragOffset = _k[1];
var _l = (0, react_1.useState)(false), isSending = _l[0], setIsSending = _l[1];
var textareaRef = (0, react_1.useRef)(null);
var handleMouseDown = function (e) {
var _a;
if (e.target instanceof HTMLTextAreaElement ||
e.target instanceof HTMLButtonElement)
return;
e.preventDefault();
e.stopPropagation();
// console.log('DraggableNote handleMouseDown', id, isDraggingNew);
// If this is a new note being placed, don't start dragging
if (isFollowingMouse || isDraggingNew) {
return;
}
// Only start dragging if it's an existing note
var element = ref;
var rect = (_a = element.current) === null || _a === void 0 ? void 0 : _a.getBoundingClientRect();
if (!rect)
return;
setDragOffset({
x: e.clientX - rect.left,
y: e.clientY - rect.top
});
setIsDragging(true);
};
// Initialize auto-dragging for new notes
(0, react_1.useEffect)(function () {
if (isFollowingMouse || isDraggingNew) {
setIsDragging(true);
}
else {
setIsDragging(false);
}
}, [isFollowingMouse, isDraggingNew]);
var handleMouseMove = function (e) {
if (!isDragging && !isFollowingMouse)
return;
e.preventDefault();
var scrollX = window.scrollX;
var scrollY = window.scrollY;
var newPosition = {
x: e.clientX + scrollX - 150, // Center horizontally
y: e.clientY + scrollY - 100 // Center vertically
};
setPosition(newPosition);
onPositionChange === null || onPositionChange === void 0 ? void 0 : onPositionChange(newPosition);
};
var handleMouseUp = function (e) {
if (!isDragging && !isFollowingMouse)
return;
// Only stop dragging if it's not a new note or if we're explicitly placing it
if (!isDraggingNew) {
setIsDragging(false);
setIsFollowingMouse(false);
}
else if (isDraggingNew && isFollowingMouse) {
// If it's a new note being placed, let the parent component handle it
// We'll just update our local state
setIsDragging(false);
setIsFollowingMouse(false);
// The parent will handle setIsDraggingNew via the click handler
}
};
// Add a click handler to ensure the note can be placed
var handleClick = function (e) {
// console.log('DraggableNote handleClick', id, isDraggingNew, isFollowingMouse);
if (isDraggingNew && isFollowingMouse) {
e.stopPropagation();
setIsDraggingNew === null || setIsDraggingNew === void 0 ? void 0 : setIsDraggingNew(false);
setIsFollowingMouse(false);
setIsDragging(false);
// Focus the textarea after placing the note
if (textareaRef.current) {
setTimeout(function () {
var _a;
(_a = textareaRef.current) === null || _a === void 0 ? void 0 : _a.focus();
}, 100);
}
}
};
// Remove the old mouse following effect since we're handling it in handleMouseMove
(0, react_1.useEffect)(function () {
if (isDragging || isFollowingMouse) {
window.addEventListener('mousemove', handleMouseMove);
window.addEventListener('mouseup', handleMouseUp);
}
return function () {
window.removeEventListener('mousemove', handleMouseMove);
window.removeEventListener('mouseup', handleMouseUp);
};
}, [isDragging, isFollowingMouse]);
var handleContentChange = function (e) {
var newContent = e.target.value;
setContent(newContent);
onContentChange === null || onContentChange === void 0 ? void 0 : onContentChange(newContent);
};
var handleSendEmail = function () { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!content.trim()) {
react_toastify_1.toast.error('Please add some content before sending');
return [2 /*return*/];
}
setIsSending(true);
_a.label = 1;
case 1:
_a.trys.push([1, , 3, 4]);
return [4 /*yield*/, (onSendEmail === null || onSendEmail === void 0 ? void 0 : onSendEmail(content))];
case 2:
_a.sent();
return [3 /*break*/, 4];
case 3:
setIsSending(false);
return [7 /*endfinally*/];
case 4: return [2 /*return*/];
}
});
}); };
return (react_1.default.createElement("div", { ref: ref, "data-note-id": id, className: "sticky-note-container absolute ".concat(color, " rounded-lg shadow-lg cursor-move min-w-[300px] min-h-[200px] \n ").concat(isDragging || isFollowingMouse ? 'shadow-xl scale-[1.02]' : '', " \n transform transition-all duration-200 ease-out"), style: {
left: position.x,
top: position.y,
zIndex: isDragging || isFollowingMouse ? 9999 : 1000,
opacity: isFollowingMouse ? 0.8 : 1,
pointerEvents: 'auto' // Always enable pointer events
}, onMouseDown: handleMouseDown, onClick: handleClick },
react_1.default.createElement("div", { className: "flex items-center justify-between p-3 border-b border-yellow-200 bg-yellow-50 rounded-t-lg", onClick: function (e) {
if (isDraggingNew && isFollowingMouse) {
e.stopPropagation();
setIsDraggingNew === null || setIsDraggingNew === void 0 ? void 0 : setIsDraggingNew(false);
setIsFollowingMouse(false);
setIsDragging(false);
}
} },
react_1.default.createElement("div", { className: "flex space-x-2" },
react_1.default.createElement("div", { className: "w-3 h-3 rounded-full bg-red-400" }),
react_1.default.createElement("div", { className: "w-3 h-3 rounded-full bg-yellow-400" }),
react_1.default.createElement("div", { className: "w-3 h-3 rounded-full bg-green-400" })),
react_1.default.createElement("div", { className: "flex space-x-2" }, onClose && (react_1.default.createElement("button", { onClick: onClose, className: "text-gray-600 hover:text-red-600 transition-colors", title: "Close note" },
react_1.default.createElement("svg", { className: "w-4 h-4", fill: "none", stroke: "currentColor", viewBox: "0 0 24 24" },
react_1.default.createElement("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: "2", d: "M6 18L18 6M6 6l12 12" })))))),
react_1.default.createElement("div", { className: "p-4 flex flex-col items-end" },
react_1.default.createElement("textarea", { ref: textareaRef, className: "w-full h-[150px] bg-transparent resize-none outline-none text-gray-700 placeholder-gray-400", value: content, onChange: handleContentChange, placeholder: "Type your note here...", autoFocus: !isFollowingMouse && autoFocus }),
react_1.default.createElement("button", { onClick: handleSendEmail, className: "text-gray-600 hover:text-blue-600 transition-colors ".concat(isSending ? 'opacity-50 cursor-not-allowed' : ''), title: "Send as email", disabled: isSending, style: {
display: 'flex',
justifyContent: 'flex-end',
// width: '95%'
} },
react_1.default.createElement("svg", { className: "w-10 h-10 ".concat(isSending ? 'animate-spin' : ''), fill: "none", stroke: "currentColor", viewBox: "0 0 24 24" }, isSending ? (react_1.default.createElement("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: "2", d: "M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" })) : (react_1.default.createElement("path", { strokeLinecap: "round", strokeLinejoin: "round", strokeWidth: "2", d: "M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" })))))));
});
exports.DraggableNote.displayName = 'DraggableNote';
;