Created
October 17, 2019 15:08
-
-
Save darth-cheney/3c71b4b0910698f4045fba596a421990 to your computer and use it in GitHub Desktop.
Possible Sheet Frame Stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component} from './Component'; | |
import {h} from 'maquette'; | |
class ESheet extends Component { | |
constructor(props, ...args){ | |
super(props, ...args); | |
// Component-specific properties | |
this.cursorIndex = [0, 0]; | |
this.currentFrame = null; | |
this._dataCache = new SheetDataCache(props.maxColumns, props.maxRows); | |
// Etc, etc, etc | |
} | |
}; | |
/** | |
* Holds an internal dict of points | |
* and their values | |
*/ | |
class SheetDataCache { | |
constructor(maxColumns, maxRows){ | |
this.maxColumns = maxColumns; | |
this.maxRows = maxRows; | |
this._cache = {}; | |
// Bind methods | |
this.loadForFrame = this.loadForFrame.bind(this); | |
this.storeFromFrame = this.storeFromFrame.bind(this); | |
this._getValidFrame = this._getValidFrame.bind(this); | |
} | |
/** | |
* Return a 2d array structure corresponding | |
* to values pulled out of the cache for the given | |
* Frame | |
*/ | |
loadForFrame(aFrame){ | |
let result = new Array(aFrame.numColumns); | |
aFrame.forEach((colIdx, rowIdx) => { | |
let column = result[colIdx]; | |
if(column == undefined){ | |
column = new Array(aFrame.numRows); | |
result[colIdx] = column; | |
} | |
let cacheIndex = [colIdx, rowIdx]; | |
let cachedItem = this._cache[cacheIndex]; | |
result[colIdx][rowIdx] = cachedItem; | |
}); | |
return result; | |
} | |
/** | |
* Store the incoming 2d array structure's values | |
* into the cache at coordinates transposed | |
* by the Frame | |
*/ | |
storeFromFrame(aFrame, structure){ | |
let columns = new Array(aFrame.numColumns); | |
columns.forEach((relColIdx, relRowIdx, colIdx, rowIdx) => { | |
let itemValue = structure[colIdx][rowIdx]; | |
let cacheIndex = [relColIdx, relRowIdx]; | |
this._cache[cacheIndex] = itemValue; | |
}); | |
} | |
_getValidFrame(aFrame){ | |
if(aFrame.corner.x >= this.maxColumns){ | |
aFrame.corner.x = this.maxColumns - 1; | |
} | |
if(aFrame.corner.y >= this.maxRows){ | |
aFrame.corner.y = this.maxRows - 1; | |
} | |
return aFrame; | |
} | |
} | |
/** | |
* SheetFram does not store data, but can | |
* apply operations over 2d array structures | |
* and also, hopefully, do some Rect / Point | |
* math operations | |
*/ | |
class SheetFrame { | |
constructor(origin, corner){ | |
this.origin = origin; | |
this.corner = corner; | |
// Bind methods | |
this.forEach = this.forEach.bind(this); | |
this.getEmptyFrame = this.getEmptyFrame.bind(this); | |
} | |
/** | |
* Loop through all potential points in | |
* specified by this Frame's dimensions | |
* and apply to a callback both the relative (Frame adjusted) | |
* column and rows, as well as the index of the counts | |
* for each loop as colIds and rowIdx | |
*/ | |
forEach(cb){ | |
for(let colIdx = 0; colIdx < this.numColumns; colIdx++){ | |
let relativeColIdx = colIdx + this.origin.x; | |
for(let rowIdx = 0; rowIdx < this.numRows; rowIdx++){ | |
let relativeRowIdx = rowIdx + this.origin.y; | |
cb(relativeColIdx, relativeRowIdx, colIdx, rowIdx); | |
} | |
} | |
} | |
/* Maybe Not needed? */ | |
getEmptyFrameData(){ | |
let columns = new Array(this.numColumns); | |
return columns.map(col => { | |
return new Array(this.numRows); | |
}); | |
} | |
get numColumns(){ | |
return (this.corner.x - this.origin.x) + 1; | |
} | |
get numRows(){ | |
return (this.corner.y - this.origin.y) + 1; | |
} | |
} | |
class Point { | |
constructor(x, y){ | |
this._coordinates = [x, y]; | |
} | |
get x(){ | |
return this._coordinates[0]; | |
} | |
get y(){ | |
return this._coordinates[1]; | |
} | |
set x(val){ | |
this._coordinates[0] = val; | |
} | |
set y(val){ | |
this._coordinates[1] = val; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment