summaryrefslogtreecommitdiff
path: root/extension/react-app/src/components/EditableDiv.tsx
blob: 1c295843471abc1c1b0521d8fd34fb68b1d6a08f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import styled from "styled-components";
import {
  defaultBorderRadius,
  lightGray,
  secondaryDark,
  vscForeground,
} from ".";
import { getFontSize } from "../util";

const Div = styled.div<{ fontSize?: number }>`
  resize: none;

  padding: 8px;
  font-size: ${(props) => props.fontSize || getFontSize()}px;
  font-family: inherit;
  border-radius: ${defaultBorderRadius};
  margin: 8px auto;
  height: auto;
  width: 100%;
  background-color: ${secondaryDark};
  color: ${vscForeground};
  z-index: 1;
  border: 1px solid transparent;

  &:focus {
    outline: 1px solid ${lightGray};
    border: 1px solid transparent;
  }

  &::placeholder {
    color: ${lightGray}80;
  }
`;

const Span = styled.span<{ color?: string }>`
  background-color: ${(props) => props.color || "#2cf8"};
  border-radius: ${defaultBorderRadius};
  padding: 2px 4px;
`;

interface EditableDivProps {
  onChange: (e: any) => void;
  value?: string;
}

function EditableDiv(props: EditableDivProps) {
  return (
    <Div
      fontSize={getFontSize()}
      suppressContentEditableWarning={true}
      contentEditable={true}
      onChange={(e) => {
        const target = e.target as HTMLTextAreaElement;
        // Update the height of the textarea to match the content, up to a max of 200px.
        target.style.height = "auto";
        target.style.height = `${Math.min(
          target.scrollHeight,
          300
        ).toString()}px`;

        // setShowContextDropdown(target.value.endsWith("@"));
        props.onChange(e);
      }}
      onKeyDown={(e) => {
        // if (e.key === "Delete") {
        //   // Delete spans if they are last child
        //   const selection = window.getSelection();
        //   const range = selection?.getRangeAt(0);
        //   const node = range?.startContainer;
        //   console.log("Del");
        //   if (node?.nodeName === "SPAN") {
        //     console.log("span");
        //     const parent = node.parentNode;
        //     if (parent?.childNodes.length === 1) {
        //       parent.removeChild(node);
        //     }
        //   }
        // }
      }}
    >
      {props.value ? props.value : <Span contentEditable={false}>testing</Span>}
    </Div>
  );
}

export default EditableDiv;