Index: trunk/parsers/wikidom/lib/es/es.ParagraphBlock.js |
— | — | @@ -1,7 +1,8 @@ |
2 | 2 | /** |
| 3 | + * es.ParagraphBlock |
3 | 4 | * |
4 | 5 | * @extends {es.Block} |
5 | | - * @param lines {Array} List of line objects |
| 6 | + * @param lines {Array} List of Wikidom line objects |
6 | 7 | * @returns {es.ParagraphBlock} |
7 | 8 | */ |
8 | 9 | es.ParagraphBlock = function( lines ) { |
— | — | @@ -14,12 +15,20 @@ |
15 | 16 | this.flow.on( 'render', function() { |
16 | 17 | block.emit( 'update' ); |
17 | 18 | } ); |
18 | | -} |
| 19 | +}; |
19 | 20 | |
| 21 | +/** |
| 22 | + * Creates a new list block object from Wikidom data. |
| 23 | + * |
| 24 | + * @param wikidomList {Object} Wikidom data to convert from |
| 25 | + */ |
20 | 26 | es.ParagraphBlock.newFromWikidom = function( wikidomBlock ) { |
21 | 27 | return new es.ParagraphBlock( wikidomBlock.lines ); |
22 | 28 | }; |
23 | 29 | |
| 30 | +/** |
| 31 | + * Gets the length of all block content. |
| 32 | + */ |
24 | 33 | es.ParagraphBlock.prototype.getLength = function() { |
25 | 34 | return this.content.getLength(); |
26 | 35 | }; |
— | — | @@ -51,6 +60,20 @@ |
52 | 61 | }; |
53 | 62 | |
54 | 63 | /** |
| 64 | + * Applies an annotation to a given range. |
| 65 | + * |
| 66 | + * If a range arguments are not provided, all content will be annotated. |
| 67 | + * |
| 68 | + * @param method {String} Way to apply annotation ("toggle", "add" or "remove") |
| 69 | + * @param annotation {Object} Annotation to apply |
| 70 | + * @param start {Integer} Offset to begin annotating from |
| 71 | + * @param end {Integer} Offset to stop annotating to |
| 72 | + */ |
| 73 | +es.ParagraphBlock.prototype.annotateContent = function( method, annotation, start, end ) { |
| 74 | + this.content.annotate( method, annotation, start, end ); |
| 75 | +}; |
| 76 | + |
| 77 | +/** |
55 | 78 | * Gets content within a range. |
56 | 79 | * |
57 | 80 | * @param start {Integer} Offset to get content from |
— | — | @@ -99,20 +122,6 @@ |
100 | 123 | }; |
101 | 124 | |
102 | 125 | /** |
103 | | - * Applies an annotation to a given range. |
104 | | - * |
105 | | - * If a range arguments are not provided, all content will be annotated. |
106 | | - * |
107 | | - * @param method {String} Way to apply annotation ("toggle", "add" or "remove") |
108 | | - * @param annotation {Object} Annotation to apply |
109 | | - * @param start {Integer} Offset to begin annotating from |
110 | | - * @param end {Integer} Offset to stop annotating to |
111 | | - */ |
112 | | -es.ParagraphBlock.prototype.annotateContent = function( method, annotation, start, end ) { |
113 | | - this.content.annotate( method, annotation, start, end ); |
114 | | -}; |
115 | | - |
116 | | -/** |
117 | 126 | * Gets the start and end points of the word closest a given offset. |
118 | 127 | * |
119 | 128 | * @param offset {Integer} Offset to find word nearest to |
— | — | @@ -134,6 +143,9 @@ |
135 | 144 | return new es.Range( 0, this.content.getLength() ); |
136 | 145 | }; |
137 | 146 | |
138 | | -es.Block.models['paragraph'] = es.ParagraphBlock; |
| 147 | +/** |
| 148 | + * Extend es.Block to support paragraph block creation with es.Block.newFromWikidom |
| 149 | + */ |
| 150 | +es.Block.models.paragraph = es.ParagraphBlock; |
139 | 151 | |
140 | | -es.extend( es.ParagraphBlock, es.Block ); |
\ No newline at end of file |
| 152 | +es.extend( es.ParagraphBlock, es.Block ); |
Index: trunk/parsers/wikidom/lib/es/es.ListBlock.js |
— | — | @@ -1,5 +1,10 @@ |
2 | 2 | /**
|
3 | | - * es.ListBlock |
| 3 | + * es.ListBlock
|
| 4 | + *
|
| 5 | + * @extends {es.Block}
|
| 6 | + * @param style {String} Type of list, either "number" or "bullet"
|
| 7 | + * @param items {Array} List of es.ListBlockItems to append initially to the root list
|
| 8 | + * @return {es.ListBlock} |
4 | 9 | */
|
5 | 10 | es.ListBlock = function( style, items ) {
|
6 | 11 | es.Block.call( this );
|
— | — | @@ -11,28 +16,119 @@ |
12 | 17 | this.list.on( 'update', function() {
|
13 | 18 | block.emit( 'update' );
|
14 | 19 | } );
|
15 | | -}
|
| 20 | +};
|
16 | 21 |
|
| 22 | +/**
|
| 23 | + * Creates a new list block object from Wikidom data.
|
| 24 | + *
|
| 25 | + * @param wikidomList {Object} Wikidom data to convert from
|
| 26 | + */
|
17 | 27 | es.ListBlock.newFromWikidom = function( wikidomList ) {
|
18 | 28 | return new es.ListBlock( wikidomList.style, wikidomList.items );
|
19 | 29 | };
|
20 | 30 |
|
21 | | -es.ListBlock.prototype.getText = function() {
|
22 | | - return '';
|
23 | | -};
|
24 | | -
|
| 31 | +/**
|
| 32 | + * Gets the length of all block content.
|
| 33 | + */
|
25 | 34 | es.ListBlock.prototype.getLength = function() {
|
26 | 35 | // Compensate for n+1 virtual position on the last item's content
|
27 | 36 | return this.list.getLength() - 1;
|
28 | 37 | };
|
29 | 38 |
|
30 | 39 | /**
|
| 40 | + * Inserts content into a block at an offset.
|
| 41 | + *
|
| 42 | + * @param offset {Integer} Position to insert content at
|
| 43 | + * @param content {Object} Content to insert
|
| 44 | + */
|
| 45 | +es.ListBlock.prototype.insertContent = function( offset, content ) {
|
| 46 | + var location = this.list.getLocationFromOffset( offset );
|
| 47 | + location.item.flow.content.insert( location.offset, content );
|
| 48 | +};
|
| 49 | +
|
| 50 | +/**
|
| 51 | + * Deletes content in a block within a range.
|
| 52 | + *
|
| 53 | + * @param offset {Integer} Offset to start removing content from
|
| 54 | + * @param length {Integer} Offset to start removing content to
|
| 55 | + */
|
| 56 | +es.ListBlock.prototype.deleteContent = function( start, end ) {
|
| 57 | + // Normalize start/end
|
| 58 | + if ( end < start ) {
|
| 59 | + var tmp = end;
|
| 60 | + end = start;
|
| 61 | + start = tmp;
|
| 62 | + }
|
| 63 | + var location = this.list.getLocationFromOffset( start );
|
| 64 | + location.item.flow.content.remove( location.offset, location.offset + end - start );
|
| 65 | +};
|
| 66 | +
|
| 67 | +/**
|
| 68 | + * Applies an annotation to a given range.
|
| 69 | + *
|
| 70 | + * If a range arguments are not provided, all content will be annotated.
|
| 71 | + *
|
| 72 | + * @param method {String} Way to apply annotation ("toggle", "add" or "remove")
|
| 73 | + * @param annotation {Object} Annotation to apply
|
| 74 | + * @param start {Integer} Offset to begin annotating from
|
| 75 | + * @param end {Integer} Offset to stop annotating to
|
| 76 | + */
|
| 77 | +es.ListBlock.prototype.annotateContent = function( method, annotation, start, end ) {
|
| 78 | + throw 'ListBlock.annotateContent not implemented yet.';
|
| 79 | +};
|
| 80 | +
|
| 81 | +/**
|
| 82 | + * Gets content within a range.
|
| 83 | + *
|
| 84 | + * @param start {Integer} Offset to get content from
|
| 85 | + * @param end {Integer} Offset to get content to
|
| 86 | + */
|
| 87 | +es.ListBlock.prototype.getContent = function() {
|
| 88 | + // TODO: Implement me!
|
| 89 | + return new Content();
|
| 90 | +};
|
| 91 | +
|
| 92 | +/**
|
| 93 | + * Gets content as plain text within a range.
|
| 94 | + *
|
| 95 | + * @param start {Integer} Offset to start get text from
|
| 96 | + * @param end {Integer} Offset to start get text to
|
| 97 | + * @param render {Boolean} If annotations should have any influence on output
|
| 98 | + */
|
| 99 | +es.ListBlock.prototype.getText = function() {
|
| 100 | + // TODO: Implement me!
|
| 101 | + return '';
|
| 102 | +};
|
| 103 | +
|
| 104 | +/**
|
31 | 105 | * Renders content into a container.
|
32 | 106 | */
|
33 | 107 | es.ListBlock.prototype.renderContent = function( offset ) {
|
34 | 108 | this.list.renderContent( offset );
|
35 | 109 | };
|
36 | 110 |
|
| 111 | +/**
|
| 112 | + * Gets the offset of a position.
|
| 113 | + *
|
| 114 | + * @param position {Integer} Offset to translate
|
| 115 | + */
|
| 116 | +es.ListBlock.prototype.getOffset = function( position ) {
|
| 117 | + if ( position.top < 0 ) {
|
| 118 | + return 0;
|
| 119 | + } else if ( position.top >= this.$.height() ) {
|
| 120 | + return this.getLength();
|
| 121 | + }
|
| 122 | + var blockOffset = this.$.offset();
|
| 123 | + position.top += blockOffset.top;
|
| 124 | + position.left += blockOffset.left;
|
| 125 | + return this.list.getOffsetFromPosition( position );
|
| 126 | +};
|
| 127 | +
|
| 128 | +/**
|
| 129 | + * Gets the position of an offset.
|
| 130 | + *
|
| 131 | + * @param offset {Integer} Offset to translate
|
| 132 | + */
|
37 | 133 | es.ListBlock.prototype.getPosition = function( offset ) {
|
38 | 134 | var location = this.list.getLocationFromOffset( offset )
|
39 | 135 | position = location.item.flow.getPosition( location.offset ),
|
— | — | @@ -52,9 +148,42 @@ |
53 | 149 | return position;
|
54 | 150 | };
|
55 | 151 |
|
| 152 | +/**
|
| 153 | + * Gets the start and end points of the word closest a given offset.
|
| 154 | + *
|
| 155 | + * @param offset {Integer} Offset to find word nearest to
|
| 156 | + * @return {Object} Range object of boundaries
|
| 157 | + */
|
| 158 | +es.ListBlock.prototype.getWordBoundaries = function( offset ) {
|
| 159 | + var location = this.list.getLocationFromOffset( offset );
|
| 160 | + var boundaries = location.item.flow.content.getWordBoundaries( location.offset );
|
| 161 | + boundaries.start += offset - location.offset;
|
| 162 | + boundaries.end += offset - location.offset;
|
| 163 | + return boundaries;
|
| 164 | +};
|
| 165 | +
|
| 166 | +/**
|
| 167 | + * Gets the start and end points of the section closest a given offset.
|
| 168 | + *
|
| 169 | + * @param offset {Integer} Offset to find section nearest to
|
| 170 | + * @return {Object} Range object of boundaries
|
| 171 | + */
|
| 172 | +es.ListBlock.prototype.getSectionBoundaries = function( offset ) {
|
| 173 | + var location = this.list.getLocationFromOffset( offset ),
|
| 174 | + start = offset - location.offset;
|
| 175 | + return new es.Range( start, start + location.item.content.getLength() );
|
| 176 | +};
|
| 177 | +
|
| 178 | +/**
|
| 179 | + * Iteratively execute a callback on each item in the list.
|
| 180 | + *
|
| 181 | + * Traversal is performed in a depth-first pattern, which is equivilant to a vertical scan of list
|
| 182 | + * items. To stop traversal, return false within the callback function.
|
| 183 | + *
|
| 184 | + * @param callback {Function} Function to execute for each item, accepts an item and index argument
|
| 185 | + * @return {Boolean} Whether all items were traversed, or traversal was cut short
|
| 186 | + */
|
56 | 187 | es.ListBlock.prototype.traverseItems = function( callback ) {
|
57 | | - // Recursively walk the tree of list items and their lists, depth first, until we get to the
|
58 | | - // same item as location.item, incrementing position.line for each line that occurs before it
|
59 | 188 | var stack = [{ 'list': this.list, 'index': 0 }],
|
60 | 189 | list,
|
61 | 190 | item,
|
— | — | @@ -89,48 +218,9 @@ |
90 | 219 | return true;
|
91 | 220 | };
|
92 | 221 |
|
93 | | -es.ListBlock.prototype.getOffset = function( position ) {
|
94 | | - if ( position.top < 0 ) {
|
95 | | - return 0;
|
96 | | - } else if ( position.top >= this.$.height() ) {
|
97 | | - return this.getLength();
|
98 | | - }
|
99 | | - var blockOffset = this.$.offset();
|
100 | | - position.top += blockOffset.top;
|
101 | | - position.left += blockOffset.left;
|
102 | | - return this.list.getOffsetFromPosition( position );
|
103 | | -};
|
| 222 | +/**
|
| 223 | + * Extend es.Block to support list block creation with es.Block.newFromWikidom
|
| 224 | + */
|
| 225 | +es.Block.models.list = es.ListBlock;
|
104 | 226 |
|
105 | | -es.ListBlock.prototype.insertContent = function( offset, content ) {
|
106 | | - var location = this.list.getLocationFromOffset( offset );
|
107 | | - location.item.flow.content.insert( location.offset, content );
|
108 | | -};
|
109 | | -
|
110 | | -es.ListBlock.prototype.deleteContent = function( start, end ) {
|
111 | | - // Normalize start/end
|
112 | | - if ( end < start ) {
|
113 | | - var tmp = end;
|
114 | | - end = start;
|
115 | | - start = tmp;
|
116 | | - }
|
117 | | - var location = this.list.getLocationFromOffset( start );
|
118 | | - location.item.flow.content.remove( location.offset, location.offset + end - start );
|
119 | | -};
|
120 | | -
|
121 | | -es.ListBlock.prototype.getWordBoundaries = function( offset ) {
|
122 | | - var location = this.list.getLocationFromOffset( offset );
|
123 | | - var boundaries = location.item.flow.content.getWordBoundaries( location.offset );
|
124 | | - boundaries.start += offset - location.offset;
|
125 | | - boundaries.end += offset - location.offset;
|
126 | | - return boundaries;
|
127 | | -};
|
128 | | -
|
129 | | -es.ListBlock.prototype.getSectionBoundaries = function( offset ) {
|
130 | | - var location = this.list.getLocationFromOffset( offset ),
|
131 | | - start = offset - location.offset;
|
132 | | - return new es.Range( start, start + location.item.content.getLength() );
|
133 | | -};
|
134 | | -
|
135 | | -es.Block.models['list'] = es.ListBlock;
|
136 | | -
|
137 | 227 | es.extend( es.ListBlock, es.Block );
|
Index: trunk/parsers/wikidom/lib/es/es.Block.js |
— | — | @@ -1,4 +1,7 @@ |
2 | 2 | /** |
| 3 | + * Base object for all blocks, providing basic shared functionality and stubs for required |
| 4 | + * implementations. |
| 5 | + * |
3 | 6 | * @extends {es.EventEmitter} |
4 | 7 | * @returns {es.Block} |
5 | 8 | */ |
— | — | @@ -8,12 +11,17 @@ |
9 | 12 | } |
10 | 13 | |
11 | 14 | /** |
12 | | - * Association between block type name and block class |
13 | | - * Example: "paragraph" => es.ParagraphBlock |
| 15 | + * Association between block type-name and block constructor |
14 | 16 | * |
| 17 | + * @example "paragraph" => es.ParagraphBlock |
15 | 18 | */ |
16 | 19 | es.Block.models = {}; |
17 | 20 | |
| 21 | +/** |
| 22 | + * Creates a new block object from Wikidom data. |
| 23 | + * |
| 24 | + * @param wikidomBlock {Object} Wikidom data to convert from |
| 25 | + */ |
18 | 26 | es.Block.newFromWikidom = function( wikidomBlock ) { |
19 | 27 | if ( wikidomBlock.type in es.Block.models ) { |
20 | 28 | return es.Block.models[wikidomBlock.type].newFromWikidom( wikidomBlock ); |
— | — | @@ -22,10 +30,6 @@ |
23 | 31 | } |
24 | 32 | }; |
25 | 33 | |
26 | | -es.Block.prototype.getLength = function() { |
27 | | - throw 'Block.getLength not implemented in this subclass.'; |
28 | | -}; |
29 | | - |
30 | 34 | /** |
31 | 35 | * Gets the index of the block within it's document. |
32 | 36 | * |
— | — | @@ -65,6 +69,13 @@ |
66 | 70 | }; |
67 | 71 | |
68 | 72 | /** |
| 73 | + * Gets the length of all block content. |
| 74 | + */ |
| 75 | +es.Block.prototype.getLength = function() { |
| 76 | + throw 'Block.getLength not implemented in this subclass.'; |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
69 | 80 | * Inserts content into a block at an offset. |
70 | 81 | * |
71 | 82 | * @param offset {Integer} Position to insert content at |
— | — | @@ -85,6 +96,20 @@ |
86 | 97 | }; |
87 | 98 | |
88 | 99 | /** |
| 100 | + * Applies an annotation to a given range. |
| 101 | + * |
| 102 | + * If a range arguments are not provided, all content will be annotated. |
| 103 | + * |
| 104 | + * @param method {String} Way to apply annotation ("toggle", "add" or "remove") |
| 105 | + * @param annotation {Object} Annotation to apply |
| 106 | + * @param start {Integer} Offset to begin annotating from |
| 107 | + * @param end {Integer} Offset to stop annotating to |
| 108 | + */ |
| 109 | +es.Block.prototype.annotateContent = function( method, annotation, start, end ) { |
| 110 | + throw 'Block.annotateContent not implemented in this subclass.'; |
| 111 | +}; |
| 112 | + |
| 113 | +/** |
89 | 114 | * Gets content within a range. |
90 | 115 | * |
91 | 116 | * @param start {Integer} Offset to get content from |
— | — | @@ -131,20 +156,6 @@ |
132 | 157 | }; |
133 | 158 | |
134 | 159 | /** |
135 | | - * Applies an annotation to a given range. |
136 | | - * |
137 | | - * If a range arguments are not provided, all content will be annotated. |
138 | | - * |
139 | | - * @param method {String} Way to apply annotation ("toggle", "add" or "remove") |
140 | | - * @param annotation {Object} Annotation to apply |
141 | | - * @param start {Integer} Offset to begin annotating from |
142 | | - * @param end {Integer} Offset to stop annotating to |
143 | | - */ |
144 | | -es.Block.prototype.annotateContent = function( method, annotation, start, end ) { |
145 | | - throw 'Block.annotateContent not implemented in this subclass.'; |
146 | | -}; |
147 | | - |
148 | | -/** |
149 | 160 | * Gets the start and end points of the word closest a given offset. |
150 | 161 | * |
151 | 162 | * @param offset {Integer} Offset to find word nearest to |