77 'dragdrop' ,
88 'editables' ,
99 'autoformat' ,
10+ 'boundaries' ,
1011 'events' ,
1112 'functions' ,
1213 'keys' ,
@@ -23,6 +24,7 @@ define([
2324 DragDrop ,
2425 Editables ,
2526 AutoFormat ,
27+ Boundaries ,
2628 Events ,
2729 Fn ,
2830 Keys ,
@@ -37,14 +39,92 @@ define([
3739 var doc = document ;
3840 var win = Dom . documentWindow ( doc ) ;
3941
42+ function getBoundaries ( type , nativeEvent , target ) {
43+ var doc = target . document || target . ownerDocument ;
44+ if ( ( 'mousedown' === type || 'click' === type ) && nativeEvent && doc ) {
45+ return Boundaries . fromPosition (
46+ nativeEvent . clientX ,
47+ nativeEvent . clientY ,
48+ doc
49+ ) ;
50+ }
51+ if ( 'aloha' === type || 'mahalo' === type ) {
52+ return [ Boundaries . create ( target , 0 ) , Boundaries . create ( target , 0 ) ] ;
53+ }
54+ return doc && Boundaries . get ( doc ) ;
55+ }
56+
57+ /**
58+ * Creates an event object that will contain the following properties:
59+ * type
60+ * target
61+ * editor
62+ * boundaries
63+ *
64+ * In addition to these mandatory properties, it may contain the following:
65+ * editable
66+ * nativeEvent
67+ *
68+ * @param {!Editor } editor
69+ * @param {!Event } nativeEvent
70+ * @param {!Object } custom
71+ * @return {?Event }
72+ */
73+ function createEvent ( editor , nativeEvent , custom ) {
74+ var type , target , editable , boundaries ;
75+ if ( custom ) {
76+ type = custom . type ;
77+ target = custom . target ;
78+ editable = custom . editable ;
79+ boundaries = custom . boundaries ;
80+ }
81+ if ( ! type && nativeEvent ) {
82+ type = nativeEvent . type ;
83+ if ( ! type ) {
84+ console . warn ( 'no type' ) ;
85+ return null ;
86+ }
87+ }
88+ if ( 'mousemove' === type ) {
89+ console . warn ( 'ignore mousemove' ) ;
90+ return null ;
91+ }
92+ if ( ! target && nativeEvent ) {
93+ target = nativeEvent . target ;
94+ if ( ! target ) {
95+ console . warn ( 'no target' ) ;
96+ return null ;
97+ }
98+ }
99+ if ( ! boundaries ) {
100+ boundaries = getBoundaries ( type , nativeEvent , target ) ;
101+ if ( ! boundaries ) {
102+ console . warn ( 'no boundaries' ) ;
103+ return null ;
104+ }
105+ }
106+ if ( ! editable ) {
107+ var cac = Boundaries . commonContainer ( boundaries [ 0 ] , boundaries [ 1 ] ) ;
108+ if ( Dom . isEditableNode ( cac ) ) {
109+ editable = Editables . fromBoundary ( editor , boundaries [ 0 ] ) ;
110+ }
111+ }
112+ return {
113+ type : type ,
114+ target : target ,
115+ editor : editor ,
116+ editable : editable ,
117+ boundaries : boundaries ,
118+ nativeEvent : nativeEvent
119+ } ;
120+ }
121+
40122 function editor ( nativeEvent , custom ) {
41- var event = custom || { nativeEvent : nativeEvent } ;
42- event . editor = editor ;
43- event . lastEditableBoundaries = editor . lastEditableBoundaries ;
44- event . type = event . type || ( nativeEvent && nativeEvent . type ) || 'unknown' ;
45- event = Fn . comp . apply ( editor . stack , editor . stack ) ( event ) ;
46- editor . lastEditableBoundaries = event . boundaries ;
47- Selections . update ( event ) ;
123+ var event = createEvent ( editor , nativeEvent , custom ) ;
124+ if ( event ) {
125+ event = Fn . comp . apply ( editor . stack , editor . stack ) ( event ) ;
126+ Selections . update ( event ) ;
127+ }
48128 }
49129
50130 editor . BLOCK_CLASS = 'aloha-block' ;
@@ -73,24 +153,26 @@ define([
73153 *
74154 * Also serves as short aloha.aloha.
75155 *
76- * @param {Element } element
77- * @parma {Object} options
156+ * @param {! Element } element
157+ * @parma {Object= } options
78158 */
79159 function aloha ( element , options ) {
80160 editor ( null , {
81161 type : 'aloha' ,
82- element : element ,
162+ target : element ,
83163 options : options
84164 } ) ;
85165 }
86166
167+ /**
168+ * Destroys an editable.
169+ *
170+ * @param {!Element } element
171+ */
87172 function mahalo ( element ) {
88- var editable = Editables . fromElem ( editor , element ) ;
89- Editables . close ( editable ) ;
90- Editables . dissocFromEditor ( editor , editable ) ;
91173 editor ( null , {
92- type : 'mahalo' ,
93- editable : editable
174+ type : 'mahalo' ,
175+ target : element
94176 } ) ;
95177 }
96178
0 commit comments