Skip to content

Commit 20e1412

Browse files
author
Dylan Just
committed
Fixing dom-globals version. Sugar exercises.
1 parent 09eafc6 commit 20e1412

2 files changed

Lines changed: 58 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"homepage": "https://github.com/tinymce/tinymce-code-tutorial#readme",
2121
"dependencies": {
22-
"@ephox/dom-globals": "^3.9.5",
22+
"@ephox/dom-globals": "^1.1.2",
2323
"@ephox/katamari": "^6.1.2",
2424
"@ephox/sugar": "^6.1.3"
2525
},

src/main/ts/Exercise5Sugar.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Element as SugarElement } from '@ephox/sugar';
1+
import { Element as SugarElement, Document as SugarDocument, Traverse, Insert } from '@ephox/sugar';
22

33
// TODO: remove when we upgrade this tutorial to TinyMCE 5
44
import { Element, document } from '@ephox/dom-globals';
@@ -20,7 +20,62 @@ Wrapping
2020
The first thing we need to know is how to wrap and unwrap a sugar element.
2121
*/
2222

23+
2324
const e1: Element = document.createElement('span');
2425

25-
const se1 = SugarElement.fromDom(e1);
26+
// wrapping
27+
const se1: SugarElement<Element> = SugarElement.fromDom(e1);
28+
29+
// unwrapping
30+
const e2: Element = se1.dom();
31+
32+
/*
33+
Pretty simple so far.
34+
35+
Now, above, we used `document.createElement`. We want to use Sugar functions everywhere, so we should do this instead:
36+
*/
37+
38+
const se2: SugarElement<Element> = SugarElement.fromTag('span');
39+
40+
// or
41+
42+
const se3: SugarElement<Element> = SugarElement.fromTag('span', document);
43+
44+
/*
45+
It's useful to be able to pass in a document parameter to this function, since we're often dealing with iframes as well
46+
as the main document.
47+
48+
TODO: Use SugarElement's fromHtml and fromText functions to create a few elements.
49+
*/
50+
51+
52+
/*
53+
We often have to traverse from an element to its relatives. The Traverse module has useful functions for this.
54+
*/
55+
() => {
56+
const parent: SugarElement<Element> = SugarElement.fromTag('div');
57+
const kid: SugarElement<Element> = SugarElement.fromTag('span');
58+
Insert.append(parent, kid);
59+
60+
const parent2 = Traverse.parent(kid);
61+
62+
// TODO: inspect the type of Traverse.parent and explain why that type was used.
63+
// Answer:
64+
}
65+
66+
67+
68+
() => {
69+
const parent: SugarElement<Element> = SugarElement.fromTag('div');
70+
const kid1: SugarElement<Element> = SugarElement.fromTag('span');
71+
const kid2: SugarElement<Element> = SugarElement.fromTag('span');
72+
Insert.append(parent, kid1);
73+
Insert.append(parent, kid2);
74+
75+
// TODO: starting at kid1, find kid2
76+
77+
// TODO: starting at kid2, find kid1
78+
79+
// TODO: starting at parent, find both kids
80+
}
2681

0 commit comments

Comments
 (0)