質問編集履歴
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,411 @@
|
|
1
1
|
### 実現したいこと
|
2
2
|
|
3
3
|
あるdiv要素の中にspan要素とappend(" ")で追加された空白が混在していた場合、空白を消す方法はあるのでしょうか。removeを使ってもできないですし、そもそもデベロッパーツールで挿入されている部分を見ても空白があるのかどうなのか判別できません。progateのエディタでは実際にこうしているかは分かりませんが、デベロッパーツールで見た限りではこれを実現しているように見えます。詳しい方がいらっしゃいましたらご教授の程宜しくお願いします。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
### 追記
|
8
|
+
|
9
|
+
ご指摘を受けてコードの一部を記載いたしました。contenteditable属性のついたdiv部分をクリックしていただいて文字を入力すると、空白を入力したときを除いて文字がspanで囲まれるようになっております。私の至らぬ点大変ご迷惑をおかけしました。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
```html
|
14
|
+
|
15
|
+
<!DOCTYPE html>
|
16
|
+
|
17
|
+
<html>
|
18
|
+
|
19
|
+
<head>
|
20
|
+
|
21
|
+
<meta charset="utf-8">
|
22
|
+
|
23
|
+
<script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
|
24
|
+
|
25
|
+
crossorigin="anonymous"></script>
|
26
|
+
|
27
|
+
<script src = "main.js"></script>
|
28
|
+
|
29
|
+
</head>
|
30
|
+
|
31
|
+
<body>
|
32
|
+
|
33
|
+
<div class = "pre-editor2" id = "pre-editor2" contenteditable = "true" style = "z-index: 100;"></div>
|
34
|
+
|
35
|
+
<div class = "layer text-layer"><div class = "line"></div></div>
|
36
|
+
|
37
|
+
</body>
|
38
|
+
|
39
|
+
</html>
|
40
|
+
|
41
|
+
```
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
```css
|
46
|
+
|
47
|
+
* {
|
48
|
+
|
49
|
+
font-size: 12px;
|
50
|
+
|
51
|
+
line-height: 20px;
|
52
|
+
|
53
|
+
font-family: "Menlo", "Consolas", "DejaVu Sans Mono", monospace;
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
.text-input {
|
60
|
+
|
61
|
+
position: absolute;
|
62
|
+
|
63
|
+
color: white;
|
64
|
+
|
65
|
+
font-size: 1px;
|
66
|
+
|
67
|
+
font-size: 1px;
|
68
|
+
|
69
|
+
height: 1px;
|
70
|
+
|
71
|
+
width: 1px;
|
72
|
+
|
73
|
+
padding: 0 1px;
|
74
|
+
|
75
|
+
margin: 0 -1px;
|
76
|
+
|
77
|
+
border: none;
|
78
|
+
|
79
|
+
opacity: 0;
|
80
|
+
|
81
|
+
resize: none;
|
82
|
+
|
83
|
+
white-space: pre;
|
84
|
+
|
85
|
+
user-select: text;
|
86
|
+
|
87
|
+
contain: strict;
|
88
|
+
|
89
|
+
outline: none;
|
90
|
+
|
91
|
+
caret-color: transparent;
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
.text-input-active {
|
96
|
+
|
97
|
+
z-index: 1000;
|
98
|
+
|
99
|
+
color: #000;
|
100
|
+
|
101
|
+
width: auto;
|
102
|
+
|
103
|
+
height: 20px;
|
104
|
+
|
105
|
+
opacity: 1;
|
106
|
+
|
107
|
+
font-size: 12px;
|
108
|
+
|
109
|
+
background-color: transparent;
|
110
|
+
|
111
|
+
font-size: 12px;
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
.content {
|
118
|
+
|
119
|
+
position: absolute;
|
120
|
+
|
121
|
+
height: 100%;
|
122
|
+
|
123
|
+
width: 600px;
|
124
|
+
|
125
|
+
background-color: #ddd;
|
126
|
+
|
127
|
+
cursor: text;
|
128
|
+
|
129
|
+
top: 0;
|
130
|
+
|
131
|
+
}
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
.layer {
|
136
|
+
|
137
|
+
z-index: 1;
|
138
|
+
|
139
|
+
position: absolute;
|
140
|
+
|
141
|
+
overflow: hidden;
|
142
|
+
|
143
|
+
word-wrap: normal;
|
144
|
+
|
145
|
+
white-space: pre;
|
146
|
+
|
147
|
+
height: 100%;
|
148
|
+
|
149
|
+
width: 100%;
|
150
|
+
|
151
|
+
box-sizing: border-box;
|
152
|
+
|
153
|
+
pointer-events: none;
|
154
|
+
|
155
|
+
}
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
.scroller {
|
160
|
+
|
161
|
+
position: absolute;
|
162
|
+
|
163
|
+
overflow: hidden;
|
164
|
+
|
165
|
+
user-select: none;
|
166
|
+
|
167
|
+
cursor: text;
|
168
|
+
|
169
|
+
height: 800px;
|
170
|
+
|
171
|
+
width: 500px;
|
172
|
+
|
173
|
+
top: 0;
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
.editor {
|
180
|
+
|
181
|
+
height: 800px;
|
182
|
+
|
183
|
+
width: 500px;
|
184
|
+
|
185
|
+
position: absolute;
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
.pre-editor {
|
192
|
+
|
193
|
+
height: 800px;
|
194
|
+
|
195
|
+
width: 500px;
|
196
|
+
|
197
|
+
position: absolute;
|
198
|
+
|
199
|
+
top: 0;
|
200
|
+
|
201
|
+
margin: 0;
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
.pre-editor2 {
|
208
|
+
|
209
|
+
height: 800px;
|
210
|
+
|
211
|
+
width: 500px;
|
212
|
+
|
213
|
+
position: absolute;
|
214
|
+
|
215
|
+
top: 0;
|
216
|
+
|
217
|
+
margin: 0;
|
218
|
+
|
219
|
+
z-index: 100;
|
220
|
+
|
221
|
+
opacity: 0;
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
.gutter {
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
.text-layer {
|
236
|
+
|
237
|
+
height: 1e+06px;
|
238
|
+
|
239
|
+
top: 0;
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
.cursor-layer {
|
246
|
+
|
247
|
+
z-index: 4;
|
248
|
+
|
249
|
+
top: 0;
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
.cursor {
|
256
|
+
|
257
|
+
display: block;
|
258
|
+
|
259
|
+
width: 7px;
|
260
|
+
|
261
|
+
height: 20px;
|
262
|
+
|
263
|
+
animation: flash 0.8s infinite;
|
264
|
+
|
265
|
+
position: absolute;
|
266
|
+
|
267
|
+
box-sizing: border-box;
|
268
|
+
|
269
|
+
border-left: 2px solid;
|
270
|
+
|
271
|
+
color: #444;
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
@keyframes flash {
|
278
|
+
|
279
|
+
0% {
|
280
|
+
|
281
|
+
opacity: 1;
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
50% {
|
286
|
+
|
287
|
+
opacity: 1;
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
51%{
|
292
|
+
|
293
|
+
opacity: 0;
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
100% {
|
298
|
+
|
299
|
+
opacity: 0;
|
300
|
+
|
301
|
+
}
|
302
|
+
|
303
|
+
}
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
.generated-span {
|
308
|
+
|
309
|
+
position: absolute;
|
310
|
+
|
311
|
+
top: -1000px;
|
312
|
+
|
313
|
+
left: -1000px;
|
314
|
+
|
315
|
+
white-space: nowrap;
|
316
|
+
|
317
|
+
font-size: 12px;
|
318
|
+
|
319
|
+
}
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
.line {
|
324
|
+
|
325
|
+
height: 20px;
|
326
|
+
|
327
|
+
width: 1e+06px;
|
328
|
+
|
329
|
+
line-height: 20px;
|
330
|
+
|
331
|
+
position: absolute;
|
332
|
+
|
333
|
+
top: 0;
|
334
|
+
|
335
|
+
left: 0;
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
.cjk {
|
342
|
+
|
343
|
+
height: 20px;
|
344
|
+
|
345
|
+
line-height: 20px;
|
346
|
+
|
347
|
+
vertical-align: middle;
|
348
|
+
|
349
|
+
display: inline-block;
|
350
|
+
|
351
|
+
}
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
.indentifier {
|
356
|
+
|
357
|
+
height: 20px;
|
358
|
+
|
359
|
+
line-height: 20px;
|
360
|
+
|
361
|
+
vertical-align: middle;
|
362
|
+
|
363
|
+
display: inline-block;
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
|
368
|
+
|
369
|
+
```
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
```javascript
|
374
|
+
|
375
|
+
let target = document.querySelector('.pre-editor2');
|
376
|
+
|
377
|
+
let $target = $(target);
|
378
|
+
|
379
|
+
let textlayer = document.querySelector('.text-layer');
|
380
|
+
|
381
|
+
let $textlayer = $(textlayer);
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
target.addEventListener("input",function(){
|
386
|
+
|
387
|
+
if(target.innerHTML.lastIndexOf(" ") == "-1"){
|
388
|
+
|
389
|
+
if(target.innerHTML.length > 1) {
|
390
|
+
|
391
|
+
$('.line').children().last().remove();
|
392
|
+
|
393
|
+
}
|
394
|
+
|
395
|
+
$('.line').append(`<span class = "identifier">${target.innerHTML}</span>`);
|
396
|
+
|
397
|
+
} else {
|
398
|
+
|
399
|
+
target.innerHTML = "";
|
400
|
+
|
401
|
+
$('.line').append(" ");
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
});
|
406
|
+
|
407
|
+
```
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
[codepen](https://codepen.io/soliste16/pen/VwLxmYy)
|