r90094 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90093‎ | r90094 | r90095 >
Date:22:03, 14 June 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Added rough-sketch of planned API for an edit surface and it's related components.
Modified paths:
  • /trunk/parsers/wikidom/lib/editSurface.js (added) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/editSurface.js
@@ -0,0 +1,379 @@
 2+/**
 3+ * Pixel position, a 2D position within a rendered document.
 4+ *
 5+ * @param x {Integer} Horizontal position
 6+ * @param y {Integer} Vertical position
 7+ * @returns {Position}
 8+ */
 9+function Position( x, y ) {
 10+ this.x = x || 0;
 11+ this.y = y || 0;
 12+}
 13+
 14+/**
 15+ * Content location, an offset within a block.
 16+ *
 17+ * @param block {Block} Location target
 18+ * @param offset {Integer} Location offset
 19+ * @returns {Location}
 20+ */
 21+function Location( block, offset ) {
 22+ this.block = block;
 23+ this.offset = offset || 0;
 24+}
 25+
 26+/**
 27+ * Content selection, a pair of locations.
 28+ *
 29+ * @param from {Location} Starting location
 30+ * @param to {Location} Ending location
 31+ * @returns {Selection}
 32+ */
 33+function Selection( from, to ) {
 34+ this.from = from;
 35+ this.to = to;
 36+}
 37+
 38+/**
 39+ * Ensures that "from" is before "to".
 40+ */
 41+Selection.prototype.normalize = function() {
 42+ if ( this.from.block.index() > this.to.block.index()
 43+ || ( this.from.block.index() === this.to.block.index()
 44+ && this.from.offset > this.to.offset ) ) {
 45+ var from = sel.from;
 46+ this.from = to;
 47+ this.to = from;
 48+ }
 49+};
 50+
 51+/**
 52+ * Gets all blocks selected completely, between from and to.
 53+ *
 54+ * If from and to are adjacent blocks, or the same block, the result will always be an empty array.
 55+ *
 56+ * @returns {Array} List of blocks
 57+ */
 58+Selection.prototype.through = function() {
 59+ var through = [];
 60+ if ( this.from !== this.to && this.from.nextBlock() !== this.to ) {
 61+ var next = this.from.nextBlock()
 62+ while ( next && next !== this.to ) {
 63+ through.push( next );
 64+ next = next.nextBlock();
 65+ }
 66+ }
 67+ return through;
 68+};
 69+
 70+/**
 71+ *
 72+ * @param blocks {Array} List of blocks
 73+ * @returns {Document}
 74+ */
 75+function Document( blocks ) {
 76+ this.blocks = blocks || [];
 77+}
 78+
 79+/**
 80+ * Gets the first block in the document.
 81+ *
 82+ * @returns {Block}
 83+ */
 84+Document.prototype.firstBlock = function() {
 85+ return this.blocks.length ? this.blocks[0] : null;
 86+};
 87+
 88+/**
 89+ * Gets the last block in the document.
 90+ *
 91+ * @returns {Block}
 92+ */
 93+Document.prototype.lastBlock = function() {
 94+ return this.blocks.length ? this.blocks[this.blocks.length - 1] : null;
 95+};
 96+
 97+/**
 98+ * Adds a block to the end of the document.
 99+ *
 100+ * @param {Block} Block to append
 101+ */
 102+Document.prototype.appendBlock = function( block ) {
 103+ block.document = this;
 104+ this.blocks.push( block );
 105+};
 106+
 107+/**
 108+ * Adds a block to the beginning of the document.
 109+ *
 110+ * @param {Block} Block to prepend
 111+ */
 112+Document.prototype.prependBlock = function( block ) {
 113+ block.document = this;
 114+ this.blocks.unshift( block );
 115+};
 116+
 117+/**
 118+ * Adds a block to the document after an existing block.
 119+ *
 120+ * @param {Block} Block to insert
 121+ */
 122+Document.prototype.insertBlockBefore = function( block, before ) {
 123+ block.document = this;
 124+ if ( before ) {
 125+ this.blocks.splice( before.index(), 0, block );
 126+ } else {
 127+ this.blocks.push( block );
 128+ }
 129+};
 130+
 131+/**
 132+ * Removes a block from the document.
 133+ *
 134+ * @param {Block} Block to remove
 135+ */
 136+Document.prototype.removeBlock = function( block ) {
 137+ this.blocks.splice( block.index(), 1 );
 138+ block.document = null;
 139+};
 140+
 141+/**
 142+ *
 143+ * @param document
 144+ * @returns {Block}
 145+ */
 146+function Block( document ) {
 147+ this.document = document;
 148+}
 149+
 150+/**
 151+ * Gets the index of the block within it's document.
 152+ *
 153+ * @returns {Integer} Index of block
 154+ */
 155+Block.prototype.index = function() {
 156+ return this.document.blocks.indexOf( this );
 157+};
 158+
 159+/**
 160+ * Gets the next block in the document.
 161+ *
 162+ * @returns {Block|Null} Block directly proceeding this one, or null if none exists
 163+ */
 164+Block.prototype.nextBlock = function() {
 165+ var index = this.index() + 1;
 166+ return this.document.blocks.length < index ? this.document.blocks[index] : null;
 167+};
 168+
 169+/**
 170+ * Gets the previous block in the document.
 171+ *
 172+ * @returns {Block|Null} Block directly preceding this one, or null if none exists
 173+ */
 174+Block.prototype.previousBlock = function() {
 175+ var index = this.index() - 1;
 176+ return index >= 0 ? this.document.blocks[index] : null;
 177+};
 178+
 179+/**
 180+ * Inserts content into a block at an offset.
 181+ *
 182+ * @param offset {Integer} Position to insert content at
 183+ * @param content {Object} Content to insert
 184+ */
 185+Block.prototype.insertContent = function( offset, content ) {
 186+ // block-type dependent implementation
 187+};
 188+
 189+/**
 190+ * Deletes content in a block within a range.
 191+ *
 192+ * @param offset {Integer} Position to start removing content from
 193+ * @param length {Integer} Length of content to remove
 194+ */
 195+Block.prototype.deleteContent = function( offset, length ) {
 196+ // block-type dependent implementation
 197+};
 198+
 199+/**
 200+ * Replaces a range of content in a block with new content.
 201+ *
 202+ * @param offset {Integer} Position to start removing content from
 203+ * @param length {Integer} Length of content to remove
 204+ * @param content {Object} Content to insert
 205+ */
 206+Block.prototype.replaceContent = function( offset, length, content ) {
 207+ // block-type dependent implementation
 208+};
 209+
 210+/**
 211+ *
 212+ * @param $container
 213+ * @param document
 214+ * @returns {Surface}
 215+ */
 216+function Surface( $container, document ) {
 217+ this.$container = $container;
 218+ this.document = document;
 219+}
 220+
 221+/**
 222+ * Moves the cursor to a new location.
 223+ *
 224+ * @param location {Location} Location to move the cursor to
 225+ */
 226+Surface.prototype.setCursor = function( location ) {
 227+ //
 228+};
 229+
 230+/**
 231+ * Gets the current location of the cursor.
 232+ *
 233+ * @returns {Location}
 234+ */
 235+Surface.prototype.getCursor = function() {
 236+ // return location
 237+};
 238+
 239+/**
 240+ * Sets the selection to a new range.
 241+ *
 242+ * @param from {Location} Start of selection
 243+ * @param to {Location} End of selection
 244+ */
 245+Surface.prototype.setSelection = function( from, to ) {
 246+ //
 247+};
 248+
 249+/**
 250+ * Sets the selection to a new range.
 251+ *
 252+ * @param from {Selection} Selection to apply
 253+ */
 254+Surface.prototype.setSelection = function( selection ) {
 255+ //
 256+};
 257+
 258+/**
 259+ * Gets the current document selection.
 260+ *
 261+ * @returns {Selection}
 262+ */
 263+Surface.prototype.getSelection = function() {
 264+ // return selection
 265+};
 266+
 267+/**
 268+ * Gets the location of a position.
 269+ *
 270+ * @param position {Position} Position to translate
 271+ * @returns {Location}
 272+ */
 273+Surface.prototype.getLocation = function( position ) {
 274+ // return location
 275+};
 276+
 277+/**
 278+ * Gets the position of a location.
 279+ *
 280+ * @param location {Location} Location to translate
 281+ * @returns {Position}
 282+ */
 283+Surface.prototype.getPosition = function( location ) {
 284+ // return position
 285+};
 286+
 287+/**
 288+ * Moves the cursor to the nearest location directly above the current flowed line.
 289+ */
 290+Surface.prototype.moveCursorUp = function() {
 291+ var location = this.getCursor();
 292+ var below = this.getPosition( location );
 293+ var minDistance;
 294+ while ( location.block.previous() && location.offset > 0 ) {
 295+ location.offset--;
 296+ if ( location.offset === -1 ) {
 297+ location.block = block.previous();
 298+ }
 299+ var above = this.getPosition( location );
 300+ if ( above.y < below.y ) {
 301+ var distance = below.x - above.x;
 302+ if ( minDistance > distance ) {
 303+ location.offset++;
 304+ break;
 305+ } else {
 306+ minDistance = distance;
 307+ }
 308+ }
 309+ }
 310+ this.setCursor( location );
 311+};
 312+
 313+/**
 314+ * Moves the cursor to the nearest location directly below the current flowed line.
 315+ */
 316+Surface.prototype.moveCursorDown = function() {
 317+ var location = this.getCursor();
 318+ var above = this.getPosition( location );
 319+ var minDistance;
 320+ while ( location.block.next() && location.offset < location.block.length ) {
 321+ location.offset++;
 322+ if ( location.offset = location.block.length ) {
 323+ location.block = block.next();
 324+ }
 325+ var below = this.getPosition( location );
 326+ if ( above.y < below.y ) {
 327+ var distance = below.x - above.x;
 328+ if ( minDistance > distance ) {
 329+ location.offset++;
 330+ break;
 331+ } else {
 332+ minDistance = distance;
 333+ }
 334+ }
 335+ }
 336+ this.setCursor( location );
 337+};
 338+
 339+/**
 340+ * Moves the cursor backward of the current position.
 341+ */
 342+Surface.prototype.moveCursorRight = function() {
 343+ var location = this.getCursor();
 344+ if ( location.block.length < location.offset + 1 ) {
 345+ location.offset++;
 346+ } else {
 347+ var next = location.block.next();
 348+ if ( next ) {
 349+ location.block = next;
 350+ location.offset = 0;
 351+ }
 352+ }
 353+ this.setCursor( location );
 354+};
 355+
 356+/**
 357+ * Moves the cursor forward of the current position.
 358+ */
 359+Surface.prototype.moveCursorLeft = function() {
 360+ var location = this.getCursor();
 361+ if ( location.offset > 0 ) {
 362+ location.offset--;
 363+ } else {
 364+ var previous = location.block.previous();
 365+ if ( previous ) {
 366+ location.block = previous;
 367+ location.offset = location.block.length - 1;
 368+ }
 369+ }
 370+ this.setCursor( location );
 371+};
 372+
 373+/**
 374+ * Updates the rendered view.
 375+ *
 376+ * @param from Location: Where to start re-flowing from (optional)
 377+ */
 378+Surface.prototype.reflow = function( from ) {
 379+ //
 380+};
Property changes on: trunk/parsers/wikidom/lib/editSurface.js
___________________________________________________________________
Added: svn:eol-style
1381 + native
Added: svn:mime-type
2382 + text/plain

Status & tagging log