Skip to content

Commit e4763ae

Browse files
committed
This is a very new update, code runner for html
1 parent 22fd95b commit e4763ae

8 files changed

Lines changed: 174 additions & 139 deletions

File tree

pages/beyond-beginner.md

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,14 @@ const config = {
7979
const observer = new MutationObserver(
8080
(mutations) => {
8181
mutations.forEach((m) => {
82-
// console.log('Type and Added', m.type, m.addedNodes.length)
82+
console.log('Type and Added', m.type, m.addedNodes.length)
8383
})
8484
})
85-
8685
const config = { childList: true, subtree: true};
8786
const target = document.getElementById('box');
8887
observer.observe(target, config);
89-
9088
function add() {
91-
const div = document.createElement('div')
92-
div.textContent = 'New!'
93-
target.appendChild(div)
89+
const div = document.createElement('div');div.textContent = 'New!';target.appendChild(div);
9490
}
9591
</script>
9692
```
@@ -560,14 +556,9 @@ Web Workers allow you to run JavaScript in background threads, preventing blocki
560556
self.postMessage(r.toFixed(2))
561557
}
562558
`], {type: 'application/javascript'})
563-
564559
const w = new Worker(URL.createObjectURL(blob))
565560
w.postMessage(100000)
566-
567-
w.onmessage = function(e) {
568-
document.getElementById('result').textContent = 'Result: ' + e.data;
569-
w.terminate()
570-
}
561+
w.onmessage = function(e) { document.getElementById('result').textContent = 'Result: ' + e.data; w.terminate() }
571562
}
572563
</script>
573564
```
@@ -604,18 +595,11 @@ Shared workers can be accessed by multiple scripts, windows or iframes.
604595
let count = 0
605596
self.onconnect = function(e) {
606597
const p = e.ports[0]
607-
p.onmessage = function() {
608-
count++
609-
p.postMessage('Count: ' + count)
610-
}
598+
p.onmessage = function() { count++; p.postMessage('Count: ' + count); }
611599
}
612600
`], {type: 'application/javascript'})
613-
614601
const w = new SharedWorker(URL.createObjectURL(blob));
615-
616-
w.port.onmessage = function(e) {
617-
document.getElementById('sharedResult').textContent = e.data
618-
}
602+
w.port.onmessage = function(e) { document.getElementById('sharedResult').textContent = e.data }
619603
w.port.postMessage('inc')
620604
}
621605
</script>

pages/document.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ clicksStart: 1
7676
<v-clicks>
7777

7878
```js {monaco-run} {autorun: false}
79-
const element = document.querySelector(`[data-slidev-no="255"]`)
79+
const element = document.querySelector(`[data-slidev-no="256"]`)
8080
const h1 = element.querySelector('h1');
8181
// console.log(h1.textContent)
8282

@@ -136,7 +136,7 @@ You can now traverse the DOM tree using the following based the relationships be
136136
- `nodeType`, `nodeName`, `nodeValue`
137137

138138
```js {monaco-run} {autorun: false}
139-
const element = document.querySelector(`[data-slidev-no="256"]`)
139+
const element = document.querySelector(`[data-slidev-no="258"]`)
140140

141141
// console.log(element.parentNode);console.log(element.parentElement)
142142
// console.log(element.childNodes.length);console.log(element.children.length)
@@ -169,7 +169,7 @@ clicksStart: 1
169169

170170
```js {monaco-run} {autorun: false}
171171
// TODO: Fix the bug on the querySelector
172-
const table = document.querySelector(`[data-slidev-no="257"]`)
172+
const table = document.querySelector(`[data-slidev-no="259"]`)
173173
// console.log(table);
174174
// console.log(table.rows); console.log(table.tBodies); console.log(table.tHead); console.log(table.tFoot);
175175
// console.log(table.rows[0]); console.log(table.rows[0].cells); console.log(table.rows[0].cells[0]); //tbody //tr //td //th
@@ -225,7 +225,7 @@ You can search for elements in the DOM tree using the following methods:
225225
- `matches`, `closest`
226226

227227
```js {monaco-run} {autorun: false}
228-
const element = document.querySelector(`[data-slidev-no="259"]`);
228+
const element = document.querySelector(`[data-slidev-no="261"]`);
229229
// add a id, class to the element
230230
// console.log(element.id); console.log(element.className); console.log(element.tagName); console.log(element.tagName.toLowerCase());console.log(element.name);
231231
```
@@ -244,15 +244,15 @@ clicksStart: 1
244244
Important classes to understand the node properties includes - `EventTarget`, `Node`, `Element`, `HTMLElement`-`HTMLBodyElement`-other interface discussed in the [HTML Note](https://karatu.oluwasetemi.dev/85), `Document`-`HTMLDocument`-`DocumentFragment`, `CharacterData`-`Text`-`Comment`.
245245

246246
```js {monaco-run} {autorun: false}
247-
const element = document.querySelector(`[data-slidev-no="260"]`);
247+
const element = document.querySelector(`[data-slidev-no="262"]`);
248248

249249
console.dir(element)
250250
```
251251

252252
## `innerHTML`, `outerHTML`, `textContent`, `innerText`, `nodeValue` or `data`, `hidden`
253253

254254
```js {monaco-run} {autorun: false}
255-
const element = document.querySelector(`[data-slidev-no="260"]`);
255+
const element = document.querySelector(`[data-slidev-no="262"]`);
256256

257257
// console.log(element.innerHTML); console.log(element.outerHTML); console.log(element.textContent); console.log(element.innerText); console.log(element.nodeValue); console.log(element.data); console.log(element.hidden);
258258
```
@@ -272,15 +272,15 @@ clicksStart: 1
272272
Attributes are the properties of the elements. They are the key-value pairs of the elements from the [convertion]{.text-xl.font-fast.text-red title='parsed or read'} of HTML to DOM. You can add your own properties to the element but the html attributes set is fixed according to the [specification](https://html.spec.whatwg.org/){.text-gradient}.
273273

274274
```js {monaco-run} {autorun: false}
275-
const element = document.querySelector(`[data-slidev-no="261"]`);
275+
const element = document.querySelector(`[data-slidev-no="263"]`);
276276

277277
// console.log(element.getAttribute('data-slidev-no')); console.log(element.getAttribute('data-slidev-no') === element.dataset.slidevNo); console.log(element.dataset.slidevNo);
278278
```
279279

280280
## `setAttribute`, `removeAttribute`, `hasAttribute`, `attributes`
281281

282282
```js {monaco-run} {autorun: false}
283-
const element = document.querySelector(`[data-slidev-no="261"]`);
283+
const element = document.querySelector(`[data-slidev-no="263"]`);
284284

285285
// element.setAttribute('data-slidev-no', '96'); console.log(element.dataset.slidevNo);
286286
// element.removeAttribute('data-slidev-no'); console.log(element.dataset.slidevNo);
@@ -364,7 +364,7 @@ strong.appendChild(textNode1);
364364
div.appendChild(strong);
365365
div.appendChild(textNode2);
366366

367-
const element = document.querySelector(`[data-slidev-no="263"] .default`);
367+
const element = document.querySelector(`[data-slidev-no="265"] .default`);
368368
element.prepend(div);
369369
```
370370

@@ -399,7 +399,7 @@ div.appendChild(textNode2);
399399
fragment.appendChild(div);
400400
// fragment.appendChild(div.cloneNode(true));
401401

402-
const element = document.querySelector(`[data-slidev-no="264"] .default`);
402+
const element = document.querySelector(`[data-slidev-no="266"] .default`);
403403
element.prepend(fragment);
404404
```
405405

@@ -421,7 +421,7 @@ You can modify the styles and classes of the elements using the following method
421421

422422
```js {monaco-run} {autorun: false}
423423

424-
const element = document.querySelector(`[data-slidev-no="265"]`);
424+
const element = document.querySelector(`[data-slidev-no="267"]`);
425425

426426
element.style.color = 'red'; element.style.backgroundColor = 'yellow'; element.style.padding = '15px'; element.style.border = '1px solid #d6e9c6'; element.style.borderRadius = '4px';
427427
element.style.setProperty('--uno', 'p-[15px] border border-[#d6e9c6] rounded-[4px] bg-[#dff0d8] text-[#3c763d]');
@@ -449,7 +449,7 @@ You can get the size and position of the elements using the following methods:
449449
- `scrollWidth`, `scrollHeight`, `scrollLeft`, `scrollTop`
450450

451451
```js {monaco-run} {autorun: false}
452-
const element = document.querySelector(`[data-slidev-no="266"] .view-lines`);
452+
const element = document.querySelector(`[data-slidev-no="268"] .view-lines`);
453453

454454
// console.log(element.offsetWidth); console.log(element.offsetHeight); console.log(element.offsetLeft); console.log(element.offsetTop); console.log(element.offsetParent);
455455
// console.log(element.clientWidth); console.log(element.clientHeight); console.log(element.clientLeft); console.log(element.clientTop);
@@ -552,7 +552,7 @@ Remember that element appended to a page using coordinates from another element
552552

553553
```js {monaco-run} {autorun: false}
554554
// FIX: use the deprecated pageXOffset and pageYOffset with the right values
555-
const element = document.querySelector(`[data-slidev-no="270"] h1`);
555+
const element = document.querySelector(`[data-slidev-no="272"] h1`);
556556
function getCoords(elem) { let box = elem.getBoundingClientRect(); return { top: box.top + window.pageYOffset, right: box.right + window.pageXOffset, bottom: box.bottom + window.pageYOffset, left: box.left + window.pageXOffset }; }
557557
// console.log(getCoords(element));
558558
```

pages/events.md

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,20 @@ DOM properties are assigned to the event handler. They are not strings like HTML
5050
```html {hide|none|*|1|2,6|3-5|*}
5151
<button id="elem">Click me</button>
5252
<script>
53-
elem.onclick = function () {
54-
console.log('Click!')
55-
}
53+
elem.onclick = function () { console.log('Click!') }
5654
</script>
5755
```
5856

5957
<v-clicks>
6058

6159
The `addEventListener` method allows adding multiple handlers on the same event, with additional configuration options and ability to remove them with `removeEventListener`.
6260

63-
```js {monaco-run} {autorun: false}
64-
elem.addEventListener('click', function () {
65-
console.log('First handler')
66-
})
67-
elem.addEventListener('click', function () {
68-
console.log('Second handler')
69-
})
61+
```html {monaco-run} {autorun: false}
62+
<button id="elem">Click me</button>
63+
<script>
64+
elem.addEventListener('click', function () { console.log('First handler') })
65+
elem.addEventListener('click', function () { console.log('Second handler') })
66+
</script>
7067
```
7168

7269
</v-clicks>
@@ -95,7 +92,7 @@ interface AddEventListenerOptions extends EventListenerOptions {
9592
```
9693

9794
```js {monaco-run} {autorun: false}
98-
const elem = document.querySelector(`[data-slidev-no="277"] h1`)
95+
const elem = document.querySelector(`[data-slidev-no="279"] h1`)
9996
const handler = () => console.log('Click!')
10097
elem.addEventListener('click', handler, { once: true })
10198
// elem.addEventListener('mouseover', handler);
@@ -118,13 +115,11 @@ When an event happens, the browser creates an event object, puts details into it
118115
<div grid="~ cols-2" gap="2">
119116

120117
```js {monaco-run} {autorun: false}
121-
const elem = document.querySelector(`[data-slidev-no="278"] h1`)
118+
const elem = document.querySelector(`[data-slidev-no="280"] h1`)
122119

123120
elem.addEventListener('click', function (event) {
124121
// show the event type, the element and the coordinates of the click
125-
// console.log(
126-
event.type + ' at ' + event.currentTarget + ' ' + event.eventPhase,
127-
)
122+
console.log( event.type + ' at ' + event.currentTarget + ' ' + event.eventPhase, )
128123
// console.log(`Coordinates: ${event.clientX}:${event.clientY}`)
129124
console.dir(event)
130125
})
@@ -188,7 +183,7 @@ hideInToc: true
188183
<v-clicks>
189184

190185
```js {monaco-run} {autorun: false}
191-
const elem = document.querySelector(`[data-slidev-no="280"] h1`)
186+
const elem = document.querySelector(`[data-slidev-no="282"] h1`)
192187
const parent = elem.parentElement
193188
const grandParent = parent.parentElement
194189

@@ -240,7 +235,7 @@ clicksStart: 1
240235

241236
Event delegation is a technique involving adding a single event listener to a common parent rather than adding them to multiple child nodes. The idea is that if we have a lot of elements handled in a similar way, then instead of assigning a handler to each of them – we put a single handler on their common ancestor.
242237

243-
```html
238+
```html {monaco-run} {autorun: false}
244239
<ul id="menu">
245240
<li>Home</li>
246241
<li>About</li>
@@ -251,7 +246,7 @@ Event delegation is a technique involving adding a single event listener to a co
251246
menu.onclick = function (event) {
252247
let target = event.target
253248
if (target.tagName != 'LI') return
254-
// console.log(target.innerHTML)
249+
console.log(target.innerHTML)
255250
}
256251
</script>
257252
```
@@ -349,15 +344,15 @@ To prevent the default action, we can use `event.preventDefault()`. returning `f
349344

350345
<!-- prettier-ignore -->
351346
```js {monaco-run} {lineNumbers: true, autorun: false}
352-
const link = document.querySelector(`[data-slidev-no="284"] a`)
347+
const link = document.querySelector(`[data-slidev-no="286"] a`)
353348
link.addEventListener('click', function(event) {
354349
event.preventDefault(); event.stopPropagation();
355350
console.log('Link click!');
356351
});
357352
```
358353

359354
```js {monaco-run} {autorun: false}
360-
const h1 = document.querySelector(`[data-slidev-no="284"] h1`)
355+
const h1 = document.querySelector(`[data-slidev-no="286"] h1`)
361356
h1.oncontextmenu = function (event) {
362357
console.log('Content menu clicked')
363358
}
@@ -403,34 +398,48 @@ hideInToc: true
403398

404399
`event.isTrusted` - read-only property that returns a boolean value indicating whether or not the event was initiated by the browser (true) or by a script (false).
405400

406-
```html
407-
<button id="elem">Click me</button>
408-
```
401+
<div grid="~ cols-2" gap="2">
402+
<div>
409403

410-
<div flex="~">
404+
## Basic Event
411405

412-
```js
413-
let helloEvent = new Event('hello', { bubbles: true, cancelable: true })
414-
elem.addEventListener('hello', function (event) {
415-
console.log('Hello from ' + event.target.tagName)
416-
})
406+
```html {monaco-run} {autorun: false}
407+
<button id="elem">Click me</button>
408+
<script>
409+
let helloEvent = new Event('hello', {
410+
bubbles: true,
411+
cancelable: true
412+
})
413+
elem.addEventListener('hello', function (event) {
414+
console.log('Hello from ' + event.target.tagName)
415+
})
416+
// Trigger the custom event when clicked
417+
elem.addEventListener('click', function() {
418+
elem.dispatchEvent(helloEvent)
419+
})
420+
</script>
417421
```
418422

419-
<Hello />
420423
</div>
424+
<div>
421425

422-
<v-clicks>
426+
## CustomEvent with detail
423427

424428
For completely new events, we can use `CustomEvent` class. It has an additional `detail` property to pass custom data.
425429

426430
<!-- prettier-ignore-start -->
427-
```js {monaco-run} {autorun: false}
428-
const element = document.querySelector(`[data-slidev-no="286"] h1`)
429-
element.onclick = function() { element.dispatchEvent(new CustomEvent("hello", { detail: { name: "John" } })); };
430-
element.addEventListener('hello', function(event) {
431-
// console.log('Hello, ' + event.detail.name + '!, you fired ' + event.type + ' event');
432-
});
431+
```html {monaco-run} {autorun: false}
432+
<button id="elem">Click to dispatch custom event</button>
433+
<script>
434+
elem.onclick = function() {
435+
elem.dispatchEvent(new CustomEvent("hello", { detail: { name: "John" } }))
436+
}
437+
elem.addEventListener('hello', function(event) {
438+
console.log('Hello, ' + event.detail.name + '! You fired ' + event.type + ' event')
439+
})
440+
</script>
433441
```
434442
<!-- prettier-ignore-end -->
435443

436-
</v-clicks>
444+
</div>
445+
</div>

pages/modules.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ hideInToc: true
2020

2121
# Module basics
2222

23-
<div />
23+
<div class="text-sm">
2424

2525
[What is a module?]{.text-gradient.font-hand.text-10.pr-3.pl-0}
2626

@@ -30,6 +30,8 @@ Export keyword labels variables and functions that should be accessible from out
3030

3131
For instance, if we have a file `sayHi.js` exporting a function:
3232

33+
</div>
34+
3335
<div class="grid grid-cols-3 gap-4 text-sm">
3436

3537
<div>

pages/nice-to-know.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ obj = null
635635
</div>
636636
</div>
637637

638-
**Warning**: These are advanced features. Use only when necessary, as they can make code harder to reason about.
638+
<div class="text-sm"><b>Warning</b>: These are advanced features. Use only when necessary, as they can make code harder to reason about.</div>
639639

640640
---
641641
hideInToc: true

0 commit comments

Comments
 (0)