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
44import { Element , document } from '@ephox/dom-globals' ;
@@ -20,7 +20,62 @@ Wrapping
2020The first thing we need to know is how to wrap and unwrap a sugar element.
2121 */
2222
23+
2324const 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