This repository has been archived by the owner on Jul 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sc-text.js
147 lines (125 loc) · 3.18 KB
/
sc-text.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { html, css, nothing } from 'lit';
import { classMap } from 'lit/directives/class-map.js';
import ScElement from './ScElement.js';
import { fontFamily, fontSize, theme } from './styles.js';
class ScText extends ScElement {
static get properties() {
return {
width: {
type: Number,
},
height: {
type: Number,
},
value: {
type: String,
},
readonly: {
type: Boolean,
reflect: true,
},
};
}
static get styles() {
return css`
:host {
vertical-align: top;
display: inline-block;
box-sizing: border-box;
vertical-align: top;
font-size: 0;
}
textarea {
vertical-align: top;
box-sizing: border-box;
background-color: ${theme['--color-primary-2']};
border: 1px dotted ${theme['--color-primary-4']};
color: white;
font-family: ${fontFamily};
padding: 6px 2px 6px 6px;
border-radius: 2px;
font-size: ${fontSize};
line-height: ${16}px;
resize: none;
}
textarea:focus {
outline: none;
border: 1px solid ${theme['--color-primary-4']};
}
textarea.dirty {
border: 1px solid ${theme['--color-secondary-3']};
}
textarea[readonly], textarea[readonly]:focus {
background-color: ${theme['--color-primary-3']};
border: 1px solid ${theme['--color-primary-3']};
}
`;
}
constructor() {
super();
this.width = 200;
this.height = 30;
this.value = '';
this.readonly = false;
this._dirty = false;
}
render() {
const classes = { dirty: this._dirty };
return html`
<textarea
class=${classMap(classes)}
style="
width: ${this.width}px;
height: ${this.height}px;
"
.value="${this.value}"
?readonly=${this.readonly}
@blur=${this.updateValue}
@keydown=${this.onKeyDown}
@keyup=${this.onKeyUp}
@contextmenu="${this._preventContextMenu}"
></textarea>
`;
}
focus() {
const $textarea = this.shadowRoot.querySelector('textarea');
if ($textarea) {
$textarea.focus();
}
}
onKeyDown(e) {
// manually do comment because opens Help menu otherwise...
if (e.metaKey && e.key === 's') {
e.preventDefault();
this.updateValue(e, true);
}
}
onKeyUp(e) {
if (e.target.value !== this.value && this._dirty === false) {
this._dirty = true;
this.requestUpdate();
} else if (e.target.value === this.value && this._dirty === true) {
this._dirty = false;
this.requestUpdate();
}
}
updateValue(e, forceUpdate = false) {
e.preventDefault();
e.stopPropagation();
if (this._dirty || forceUpdate) {
this.value = e.target.value;
this._dirty = false;
const event = new CustomEvent('change', {
bubbles: true,
composed: true,
detail: { value: this.value },
});
this.dispatchEvent(event);
this.requestUpdate();
}
}
}
if (customElements.get('sc-text') === undefined) {
customElements.define('sc-text', ScText);
}
export default ScText;