mantine-double-click-editable 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,6 +5,9 @@ A double-click-to-edit text component for [Mantine](https://mantine.dev/), with
5
5
  [![npm version](https://img.shields.io/npm/v/mantine-double-click-editable.svg)](https://www.npmjs.com/package/mantine-double-click-editable)
6
6
  [![github](https://img.shields.io/badge/github-repo-black?logo=github)](https://github.com/ypyl/DoubleClickEditable)
7
7
 
8
+ ## Demo
9
+
10
+ ![demo](./msedge_g0RHaauDdc.gif)
8
11
 
9
12
  ## Installation
10
13
 
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m=require("react/jsx-runtime"),b=require("react"),p=require("@mantine/core");function R({children:l,onSave:a,style:u,...d}){const[i,c]=b.useState(!1),f=e=>{c(!0);const o=e.clientX,s=e.clientY;setTimeout(()=>{const n=window.getSelection();if(n){if(document.caretRangeFromPoint){const t=document.caretRangeFromPoint(o,s);t&&(n.removeAllRanges(),n.addRange(t))}else if(document.caretPositionFromPoint){const t=document.caretPositionFromPoint(o,s);if(t){const r=document.createRange();r.setStart(t.offsetNode,t.offset),r.collapse(!0),n.removeAllRanges(),n.addRange(r)}}}},0)},g=e=>{c(!1);const o=e.currentTarget.innerHTML;a?.(o)};return m.jsx(p.Text,{...d,contentEditable:i,suppressContentEditableWarning:!0,style:{border:"1px dashed #ccc",padding:"4px",cursor:i?"text":"pointer",...u},onMouseDown:e=>{e.detail>1&&e.preventDefault()},onDoubleClick:f,onBlur:g,children:l})}exports.DoubleClickEditable=R;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const x=require("react/jsx-runtime"),i=require("react"),R=require("@mantine/core");function D({children:s,onSave:d,style:f,...m}){const[r,l]=i.useState(!1),a=i.useRef(null);i.useEffect(()=>{!r&&a.current&&(a.current.innerText=s)},[s,r]);const p=e=>{l(!0);const t=e.clientX,u=e.clientY;setTimeout(()=>{const o=window.getSelection();if(o){if(document.caretRangeFromPoint){const n=document.caretRangeFromPoint(t,u);n&&(o.removeAllRanges(),o.addRange(n))}else if(document.caretPositionFromPoint){const n=document.caretPositionFromPoint(t,u);if(n){const c=document.createRange();c.setStart(n.offsetNode,n.offset),c.collapse(!0),o.removeAllRanges(),o.addRange(c)}}}},0)},g=e=>{l(!1);const t=e.currentTarget.innerText;d?.(t)},b=e=>{e.preventDefault();const t=e.clipboardData.getData("text/plain");document.execCommand("insertText",!1,t)};return x.jsx(R.Text,{...m,ref:a,contentEditable:r?"plaintext-only":!1,suppressContentEditableWarning:!0,style:{border:"1px dashed #ccc",padding:"4px",cursor:r?"text":"pointer",whiteSpace:"pre-wrap",...f},onMouseDown:e=>{e.detail>1&&e.preventDefault()},onDoubleClick:p,onBlur:g,onPaste:b})}exports.DoubleClickEditable=D;
package/dist/index.js CHANGED
@@ -1,56 +1,66 @@
1
- import { jsx as g } from "react/jsx-runtime";
2
- import { useState as p } from "react";
3
- import { Text as b } from "@mantine/core";
4
- function C({
5
- children: a,
6
- onSave: l,
1
+ import { jsx as x } from "react/jsx-runtime";
2
+ import { useState as b, useRef as R, useEffect as D } from "react";
3
+ import { Text as P } from "@mantine/core";
4
+ function T({
5
+ children: i,
6
+ onSave: u,
7
7
  style: d,
8
- ...u
8
+ ...f
9
9
  }) {
10
- const [i, c] = p(!1), m = (e) => {
11
- c(!0);
12
- const o = e.clientX, s = e.clientY;
10
+ const [r, s] = b(!1), a = R(null);
11
+ D(() => {
12
+ !r && a.current && (a.current.innerText = i);
13
+ }, [i, r]);
14
+ const m = (e) => {
15
+ s(!0);
16
+ const t = e.clientX, l = e.clientY;
13
17
  setTimeout(() => {
14
- const n = window.getSelection();
15
- if (n) {
18
+ const o = window.getSelection();
19
+ if (o) {
16
20
  if (document.caretRangeFromPoint) {
17
- const t = document.caretRangeFromPoint(o, s);
18
- t && (n.removeAllRanges(), n.addRange(t));
21
+ const n = document.caretRangeFromPoint(t, l);
22
+ n && (o.removeAllRanges(), o.addRange(n));
19
23
  } else if (document.caretPositionFromPoint) {
20
- const t = document.caretPositionFromPoint(o, s);
21
- if (t) {
22
- const r = document.createRange();
23
- r.setStart(t.offsetNode, t.offset), r.collapse(!0), n.removeAllRanges(), n.addRange(r);
24
+ const n = document.caretPositionFromPoint(t, l);
25
+ if (n) {
26
+ const c = document.createRange();
27
+ c.setStart(n.offsetNode, n.offset), c.collapse(!0), o.removeAllRanges(), o.addRange(c);
24
28
  }
25
29
  }
26
30
  }
27
31
  }, 0);
28
- }, f = (e) => {
29
- c(!1);
30
- const o = e.currentTarget.innerHTML;
31
- l?.(o);
32
+ }, p = (e) => {
33
+ s(!1);
34
+ const t = e.currentTarget.innerText;
35
+ u?.(t);
36
+ }, g = (e) => {
37
+ e.preventDefault();
38
+ const t = e.clipboardData.getData("text/plain");
39
+ document.execCommand("insertText", !1, t);
32
40
  };
33
- return /* @__PURE__ */ g(
34
- b,
41
+ return /* @__PURE__ */ x(
42
+ P,
35
43
  {
36
- ...u,
37
- contentEditable: i,
44
+ ...f,
45
+ ref: a,
46
+ contentEditable: r ? "plaintext-only" : !1,
38
47
  suppressContentEditableWarning: !0,
39
48
  style: {
40
49
  border: "1px dashed #ccc",
41
50
  padding: "4px",
42
- cursor: i ? "text" : "pointer",
51
+ cursor: r ? "text" : "pointer",
52
+ whiteSpace: "pre-wrap",
43
53
  ...d
44
54
  },
45
55
  onMouseDown: (e) => {
46
56
  e.detail > 1 && e.preventDefault();
47
57
  },
48
58
  onDoubleClick: m,
49
- onBlur: f,
50
- children: a
59
+ onBlur: p,
60
+ onPaste: g
51
61
  }
52
62
  );
53
63
  }
54
64
  export {
55
- C as DoubleClickEditable
65
+ T as DoubleClickEditable
56
66
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mantine-double-click-editable",
3
3
  "private": false,
4
- "version": "1.0.1",
4
+ "version": "1.0.3",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/ypyl/DoubleClickEditable.git"