grid-template-parser
Version:
A simple CSS Grid template parser
396 lines (331 loc) • 10.9 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["GridTemplateParser"] = factory();
else
root["GridTemplateParser"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 4);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var track = exports.track = function track() {
var start = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
var end = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
return { start: start, end: end, span: end - start };
};
var area = exports.area = function area() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref$x = _ref.x,
x = _ref$x === undefined ? 0 : _ref$x,
_ref$y = _ref.y,
y = _ref$y === undefined ? 0 : _ref$y,
_ref$width = _ref.width,
width = _ref$width === undefined ? 0 : _ref$width,
_ref$height = _ref.height,
height = _ref$height === undefined ? 0 : _ref$height;
return {
column: track(x + 1, x + width + 1),
row: track(y + 1, y + height + 1)
};
};
var rect = exports.rect = function rect() {
var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
_ref2$column = _ref2.column,
column = _ref2$column === undefined ? { start: 1, end: 1, span: 0 } : _ref2$column,
_ref2$row = _ref2.row,
row = _ref2$row === undefined ? { start: 1, end: 1, span: 0 } : _ref2$row;
return {
x: column.start - 1,
y: row.start - 1,
width: column.end - column.start,
height: row.end - row.start
};
};
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", {
value: true
});
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var find = function find(fn, direction, extremum, _ref) {
var areas = _ref.areas;
return fn.apply(undefined, _toConsumableArray(Object.keys(areas).map(function (cell) {
return areas[cell][direction][extremum];
})));
};
var minColumnStart = exports.minColumnStart = function minColumnStart(grid) {
return find(Math.min, 'column', 'start', grid);
};
var maxColumnStart = exports.maxColumnStart = function maxColumnStart(grid) {
return find(Math.max, 'column', 'start', grid);
};
var minRowStart = exports.minRowStart = function minRowStart(grid) {
return find(Math.min, 'row', 'start', grid);
};
var maxRowStart = exports.maxRowStart = function maxRowStart(grid) {
return find(Math.max, 'row', 'start', grid);
};
var minColumnEnd = exports.minColumnEnd = function minColumnEnd(grid) {
return find(Math.min, 'column', 'end', grid);
};
var maxColumnEnd = exports.maxColumnEnd = function maxColumnEnd(grid) {
return find(Math.max, 'column', 'end', grid);
};
var minRowEnd = exports.minRowEnd = function minRowEnd(grid) {
return find(Math.min, 'row', 'end', grid);
};
var maxRowEnd = exports.maxRowEnd = function maxRowEnd(grid) {
return find(Math.max, 'row', 'end', grid);
};
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.grid = undefined;
var _primitives = __webpack_require__(0);
var sep = /['"]\s*['"]?/g;
var ws = /\s+/g;
var cleanTpl = function cleanTpl(t) {
return t.trim().slice(1, -1);
};
var cleanLine = function cleanLine(l) {
return l.replace(ws, ' ').trim();
};
var getTpl = function getTpl(t) {
return cleanTpl(t).split(sep);
};
var getRow = function getRow(l) {
return cleanLine(l).split(' ');
};
var reduceLines = function reduceLines(acc, line, r) {
if (line.trim() !== '') {
getRow(line).forEach(function (area, c) {
if (area !== '.') {
if (typeof acc[area] === 'undefined') {
acc[area] = {
column: (0, _primitives.track)(c + 1, c + 2),
row: (0, _primitives.track)(r + 1, r + 2)
};
} else {
var _acc$area = acc[area],
column = _acc$area.column,
row = _acc$area.row;
column.start = Math.min(column.start, c + 1);
column.end = Math.max(column.end, c + 2);
column.span = column.end - column.start;
row.start = Math.min(row.start, r + 1);
row.end = Math.max(row.end, r + 2);
row.span = row.end - row.start;
}
}
});
}
return acc;
};
var grid = exports.grid = function grid(tpl) {
var lines = getTpl(tpl);
var width = getRow(lines[0]).length;
var height = lines.length;
var areas = lines.reduce(reduceLines, {});
return { width: width, height: height, areas: areas };
};
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var matchingArea = function matchingArea(areas, row, column) {
return function (area) {
return areas[area].row.start <= row + 1 && areas[area].row.end > row + 1 && areas[area].column.start <= column + 1 && areas[area].column.end > column + 1;
};
};
var getColumns = function getColumns(areas, grid, row) {
var current = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
var cols = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : '';
var area = areas.find(matchingArea(grid.areas, row, current));
cols += typeof area === 'string' ? area : '.';
if (current < grid.width - 1) {
return getColumns(areas, grid, row, current + 1, cols + ' ');
}
return cols;
};
var getRows = function getRows(areas, grid) {
var current = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
var rows = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
rows += '"' + getColumns(areas, grid, current) + '"';
if (current < grid.height - 1) {
return getRows(areas, grid, current + 1, rows + '\n');
}
return rows;
};
var template = exports.template = function template(grid) {
return getRows(Object.keys(grid.areas), grid);
};
/***/ }),
/* 4 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _primitives = __webpack_require__(0);
Object.defineProperty(exports, 'area', {
enumerable: true,
get: function get() {
return _primitives.area;
}
});
Object.defineProperty(exports, 'rect', {
enumerable: true,
get: function get() {
return _primitives.rect;
}
});
var _grid = __webpack_require__(2);
Object.defineProperty(exports, 'grid', {
enumerable: true,
get: function get() {
return _grid.grid;
}
});
var _template = __webpack_require__(3);
Object.defineProperty(exports, 'template', {
enumerable: true,
get: function get() {
return _template.template;
}
});
var _bounds = __webpack_require__(1);
Object.defineProperty(exports, 'minColumnStart', {
enumerable: true,
get: function get() {
return _bounds.minColumnStart;
}
});
Object.defineProperty(exports, 'maxColumnStart', {
enumerable: true,
get: function get() {
return _bounds.maxColumnStart;
}
});
Object.defineProperty(exports, 'minRowStart', {
enumerable: true,
get: function get() {
return _bounds.minRowStart;
}
});
Object.defineProperty(exports, 'maxRowStart', {
enumerable: true,
get: function get() {
return _bounds.maxRowStart;
}
});
Object.defineProperty(exports, 'minColumnEnd', {
enumerable: true,
get: function get() {
return _bounds.minColumnEnd;
}
});
Object.defineProperty(exports, 'maxColumnEnd', {
enumerable: true,
get: function get() {
return _bounds.maxColumnEnd;
}
});
Object.defineProperty(exports, 'minRowEnd', {
enumerable: true,
get: function get() {
return _bounds.minRowEnd;
}
});
Object.defineProperty(exports, 'maxRowEnd', {
enumerable: true,
get: function get() {
return _bounds.maxRowEnd;
}
});
/***/ })
/******/ ]);
});