Permalink
Browse files

Switching to UMD pattern (in progress)

  • Loading branch information...
1 parent 4f35f20 commit de482b8c4096abc924212f6982d757e653484a5e @sergeche sergeche committed Oct 15, 2013
View
@@ -7,4 +7,8 @@
/plugins/tea-for-coda
/plugins/tea-for-espresso
-.DS_Store
+.DS_Store
+/bower_components/
+/node_modules/
+/emmet.sublime-project
+/emmet.sublime-workspace
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,22 @@
+{
+ "name": "emmet",
+ "version": "1.1.0",
+ "homepage": "http://emmet.io",
+ "authors": [
+ "Sergey Chikuyonok <serge.che@gmail.com>"
+ ],
+ "description": "Emmet — the essential toolkit for web-developers",
+ "keywords": [
+ "emmet",
+ "html",
+ "css",
+ "coding",
+ "speed"
+ ],
+ "license": "MIT",
+ "private": true,
+ "dependencies": {
+ "requirejs": "~2.1.8",
+ "lodash": "~2.1.0"
+ }
+}
View
@@ -0,0 +1,128 @@
+/**
+ * Encodes/decodes image under cursor to/from base64
+ * @param {IEmmetEditor} editor
+ */
+if (typeof module === 'object' && typeof define !== 'function') {
+ var define = function (factory) {
+ module.exports = factory(require, exports, module);
+ };
+}
+
+define(function(require, exports, module) {
+ var actions = require('./main');
+ var file = require('plugin/file');
+ var base64 = require('../assets/utils/base64');
+ var actionUtils = require('../assets/utils/action');
+
+ actions.add('encode_decode_data_url', function(editor) {
+ var data = String(editor.getSelection());
+ var caretPos = editor.getCaretPos();
+
+ if (!data) {
+ // no selection, try to find image bounds from current caret position
+ var text = String(editor.getContent()), m;
+ while (caretPos-- >= 0) {
+ if (startsWith('src=', text, caretPos)) { // found <img src="">
+ if ((m = text.substr(caretPos).match(/^(src=(["'])?)([^'"<>\s]+)\1?/))) {
+ data = m[3];
+ caretPos += m[1].length;
+ }
+ break;
+ } else if (startsWith('url(', text, caretPos)) { // found CSS url() pattern
+ if ((m = text.substr(caretPos).match(/^(url\((['"])?)([^'"\)\s]+)\1?/))) {
+ data = m[3];
+ caretPos += m[1].length;
+ }
+ break;
+ }
+ }
+ }
+
+ if (data) {
+ if (startsWith('data:', data))
+ return decodeFromBase64(editor, data, caretPos);
+ else
+ return encodeToBase64(editor, data, caretPos);
+ }
+
+ return false;
+ }, {label: 'Encode\\Decode data:URL image'});
+
+ /**
+ * Test if <code>text</code> starts with <code>token</code> at <code>pos</code>
+ * position. If <code>pos</code> is omitted, search from beginning of text
+ * @param {String} token Token to test
+ * @param {String} text Where to search
+ * @param {Number} pos Position where to start search
+ * @return {Boolean}
+ * @since 0.65
+ */
+ function startsWith(token, text, pos) {
+ pos = pos || 0;
+ return text.charAt(pos) == token.charAt(0) && text.substr(pos, token.length) == token;
+ }
+
+ /**
+ * Encodes image to base64
+ *
+ * @param {IEmmetEditor} editor
+ * @param {String} imgPath Path to image
+ * @param {Number} pos Caret position where image is located in the editor
+ * @return {Boolean}
+ */
+ function encodeToBase64(editor, imgPath, pos) {
+ var editorFile = editor.getFilePath();
+ var defaultMimeType = 'application/octet-stream';
+
+ if (editorFile === null) {
+ throw "You should save your file before using this action";
+ }
+
+ // locate real image path
+ var realImgPath = file.locateFile(editorFile, imgPath);
+ if (realImgPath === null) {
+ throw "Can't find " + imgPath + ' file';
+ }
+
+ file.read(realImgPath, function(err, content) {
+ if (err) {
+ throw 'Unable to read ' + realImgPath + ': ' + err;
+ }
+
+ var b64 = base64.encode(String(content));
+ if (!b64) {
+ throw "Can't encode file content to base64";
+ }
+
+ b64 = 'data:' + (actionUtils.mimeTypes[String(file.getExt(realImgPath))] || defaultMimeType) +
+ ';base64,' + b64;
+
+ editor.replaceContent('$0' + b64, pos, pos + imgPath.length);
+ });
+
+
+ return true;
+ }
+
+ /**
+ * Decodes base64 string back to file.
+ * @param {IEmmetEditor} editor
+ * @param {String} data Base64-encoded file content
+ * @param {Number} pos Caret position where image is located in the editor
+ */
+ function decodeFromBase64(editor, data, pos) {
+ // ask user to enter path to file
+ var filePath = String(editor.prompt('Enter path to file (absolute or relative)'));
+ if (!filePath)
+ return false;
+
+ var absPath = file.createPath(editor.getFilePath(), filePath);
+ if (!absPath) {
+ throw "Can't save file";
+ }
+
+ file.save(absPath, base64.decode( data.replace(/^data\:.+?;.+?,/, '') ));
+ editor.replaceContent('$0' + filePath, pos, pos + data.length);
+ return true;
+ }
+});
@@ -0,0 +1,112 @@
+/**
+ * Move between next/prev edit points. 'Edit points' are places between tags
+ * and quotes of empty attributes in html
+ */
+if (typeof module === 'object' && typeof define !== 'function') {
+ var define = function (factory) {
+ module.exports = factory(require, exports, module);
+ };
+}
+
+define(function(require, exports, module) {
+ var actions = require('./main');
+
+ /**
+ * Search for new caret insertion point
+ * @param {IEmmetEditor} editor Editor instance
+ * @param {Number} inc Search increment: -1 — search left, 1 — search right
+ * @param {Number} offset Initial offset relative to current caret position
+ * @return {Number} Returns -1 if insertion point wasn't found
+ */
+ function findNewEditPoint(editor, inc, offset) {
+ inc = inc || 1;
+ offset = offset || 0;
+
+ var curPoint = editor.getCaretPos() + offset;
+ var content = String(editor.getContent());
+ var maxLen = content.length;
+ var nextPoint = -1;
+ var reEmptyLine = /^\s+$/;
+
+ function getLine(ix) {
+ var start = ix;
+ while (start >= 0) {
+ var c = content.charAt(start);
+ if (c == '\n' || c == '\r')
+ break;
+ start--;
+ }
+
+ return content.substring(start, ix);
+ }
+
+ while (curPoint <= maxLen && curPoint >= 0) {
+ curPoint += inc;
+ var curChar = content.charAt(curPoint);
+ var nextChar = content.charAt(curPoint + 1);
+ var prevChar = content.charAt(curPoint - 1);
+
+ switch (curChar) {
+ case '"':
+ case '\'':
+ if (nextChar == curChar && prevChar == '=') {
+ // empty attribute
+ nextPoint = curPoint + 1;
+ }
+ break;
+ case '>':
+ if (nextChar == '<') {
+ // between tags
+ nextPoint = curPoint + 1;
+ }
+ break;
+ case '\n':
+ case '\r':
+ // empty line
+ if (reEmptyLine.test(getLine(curPoint - 1))) {
+ nextPoint = curPoint;
+ }
+ break;
+ }
+
+ if (nextPoint != -1)
+ break;
+ }
+
+ return nextPoint;
+ }
+
+ /**
+ * Move caret to previous edit point
+ * @param {IEmmetEditor} editor Editor instance
+ */
+ actions.add('prev_edit_point', function(editor) {
+ var curPos = editor.getCaretPos();
+ var newPoint = findNewEditPoint(editor, -1);
+
+ if (newPoint == curPos)
+ // we're still in the same point, try searching from the other place
+ newPoint = findNewEditPoint(editor, -1, -2);
+
+ if (newPoint != -1) {
+ editor.setCaretPos(newPoint);
+ return true;
+ }
+
+ return false;
+ }, {label: 'Previous Edit Point'});
+
+ /**
+ * Move caret to next edit point
+ * @param {IEmmetEditor} editor Editor instance
+ */
+ actions.add('next_edit_point', function(editor) {
+ var newPoint = findNewEditPoint(editor, 1);
+ if (newPoint != -1) {
+ editor.setCaretPos(newPoint);
+ return true;
+ }
+
+ return false;
+ });
+});
@@ -0,0 +1,44 @@
+/**
+ * Evaluates simple math expression under caret
+ */
+if (typeof module === 'object' && typeof define !== 'function') {
+ var define = function (factory) {
+ module.exports = factory(require, exports, module);
+ };
+}
+
+define(function(require, exports, module) {
+ var actionUtils = require('../utils/action');
+ var utils = require('../utils/common');
+ var range = require('../assets/range');
+ var actions = require('./base');
+
+ actions.add('evaluate_math_expression', function(editor) {
+ var content = String(editor.getContent());
+ var chars = '.+-*/\\';
+
+ /** @type Range */
+ var sel = range(editor.getSelectionRange());
+ if (!sel.length()) {
+ sel = actionUtils.findExpressionBounds(editor, function(ch) {
+ return utils.isNumeric(ch) || chars.indexOf(ch) != -1;
+ });
+ }
+
+ if (sel && sel.length()) {
+ var expr = sel.substring(content);
+
+ // replace integral division: 11\2 => Math.round(11/2)
+ expr = expr.replace(/([\d\.\-]+)\\([\d\.\-]+)/g, 'Math.round($1/$2)');
+
+ try {
+ var result = utils.prettifyNumber(new Function('return ' + expr)());
+ editor.replaceContent(result, sel.start, sel.end);
+ editor.setCaretPos(sel.start + result.length);
+ return true;
+ } catch (e) {}
+ }
+
+ return false;
+ }, {label: 'Numbers/Evaluate Math Expression'});
+});
Oops, something went wrong.

0 comments on commit de482b8

Please sign in to comment.