(INCOMPLETE: ES6 Module conversion and linting)

- Breaking change: Require `new` with `EmbeddedSVGEdit` (allows us to use `class` internally)
- Breaking change: If `svgcanvas.setUiStrings` must now be called if not using editor in order
    to get strings (for sake of i18n) (and if using path.js alone, must also have its `setUiStrings` called)
- Breaking change (ext-overview-window): Avoid global `overviewWindowGlobals`
- Breaking change (ext-imagelib): Change to object-based encoding for namespacing of
    messages (though keep stringifying/parsing ourselves until we remove IE9 support)
- Breaking change: Rename `jquery.js` to `jquery.min.js`
- Breaking change: Remove `scoped` attribute from `style`; it is now deprecated and
    obsolete; also move to head (after other stylesheets)
- Enhancement: Make SpinButton plugin independent of SVGEdit via
    generic state object for tool_scale
- Enhancement: Remove now unused Python l10n scripts (#238)
- Enhancement: ES6 Modules (including jQuery plugins but not jQuery)
- Enhancement: Further JSDoc (incomplete)
- Enhancement (Optimization): Compress images using imageoptim (and add
    npm script) (per #215)
- Fix: i18nize path.js strings and canvas notifications
- Fix: Attempt i18n for ext-markers
- Refactoring (ext-storage): Move locale info to own file imported by the extension (toward modularity; still should be split into separate files by language and *dynamically* imported, but we'll wait for better `import` support to refactor this)
- Refactoring: For imagelib, add local jQuery copy (using old 1.4.4 as had
    been using from server)
- Refactoring: For MathJax, add local copy (using old 2.3 as had been using from
    server); server had not been working
- Refactoring: Remove `use strict` (implicit in modules)
- Refactoring: Remove trailing whitespace, fix some code within comments
- Refactoring: Expect `jQuery` global rather than `$` for better modularity
    (also to adapt line later once available via `import`)
- Refactoring: Prefer `const` (and then `let`)
- Refactoring: Add block scope keywords closer to first block in which they appear
- Refactoring: Use ES6 `class`
- Refactoring `$.isArray` -> `Array.isArray` and avoid some other jQuery core methods
    with simple VanillaJS replacements
- Refactoring: Use abbreviated object property syntax
- Refactoring: Object destructuring
- Refactoring: Remove `uiStrings` contents in svg-editor.js (obtains from locale)
- Refactoring: Add favicon to embedded API file
- Refactoring: Use arrow functions for brief functions (incomplete)
- Refactoring: Use `Array.prototype.includes`/`String.prototype.includes`;
    `String.prototype.startsWith`, `String.prototype.trim`
- Refactoring: Remove now unnecessary svgutils do/while resetting of variables
- Refactoring: Use shorthand methods for object literals (avoid ": function")
- Refactoring: Avoid quoting object property keys where unnecessary
- Refactoring: Just do truthy/falsey check for lengths in place of comparison to 0
- Refactoring (Testing): Avoid jQuery usage within most test files (defer script,
    also in preparation for future switch to ES6 modules for tests)
- Refactoring: Make jpicker variable declaration indent bearable
- Refactoring (Linting): Finish svgcanvas.js
- Docs: Mention in comment no longer an entry file as before
- Docs: Migrate old config, extensions, and FAQ docs
- Licensing: Indicate MIT is license type of rgbcolor; rename/add license file name for
    jgraduate and screencast to reflect type (Apache 2.0); rename file to reflect it
    contains license information (of type MIT) for Raphael icons
This commit is contained in:
Brett Zamir
2018-05-18 11:25:45 +08:00
parent 7cf976cfb8
commit ae2394f086
249 changed files with 24738 additions and 23260 deletions

View File

@@ -1,5 +1,4 @@
/* eslint-disable no-var */
/* globals $, svgEditor, svgedit, svgCanvas, DOMParser */
/* globals jQuery, svgEditor, svgedit, svgCanvas */
/*
* ext-imagelib.js
*
@@ -10,9 +9,8 @@
*/
svgEditor.addExtension('imagelib', function () {
'use strict';
var uiStrings = svgEditor.uiStrings;
const $ = jQuery;
const {uiStrings} = svgEditor;
$.extend(uiStrings, {
imagelib: {
@@ -24,7 +22,7 @@ svgEditor.addExtension('imagelib', function () {
}
});
var imgLibs = [
const imgLibs = [
{
name: 'Demo library (local)',
url: svgEditor.curConfig.extPath + 'imagelib/index.html',
@@ -47,7 +45,7 @@ svgEditor.addExtension('imagelib', function () {
}
function importImage (url) {
var newImage = svgCanvas.addSvgElementFromJson({
const newImage = svgCanvas.addSvgElementFromJson({
'element': 'image',
'attr': {
'x': 0,
@@ -63,58 +61,64 @@ svgEditor.addExtension('imagelib', function () {
svgCanvas.setImageURL(url);
}
var mode = 's';
var multiArr = [];
var transferStopped = false;
var pending = {};
var preview, submit;
const pending = {};
let mode = 's';
let multiArr = [];
let transferStopped = false;
let preview, submit;
window.addEventListener('message', function (evt) {
// Receive postMessage data
var response = evt.data;
// Receive `postMessage` data
let response = evt.data;
if (!response || typeof response !== 'string') { // Todo: Should namespace postMessage API for this extension and filter out here
if (!response || typeof response !== 'string') {
// Do nothing
return;
}
try { // This block can be removed if embedAPI moves away from a string to an object (if IE9 support not needed)
var res = JSON.parse(response);
if (res.namespace) { // Part of embedAPI communications
try {
// Todo: This block can be removed (and the above check changed to
// insist on an object) if embedAPI moves away from a string to
// an object (if IE9 support not needed)
response = JSON.parse(response);
if (response.namespace !== 'imagelib') {
return;
}
} catch (e) {}
} catch (e) {
return;
}
var char1 = response.charAt(0);
var id;
var svgStr;
var imgStr;
const hasName = 'name' in response;
const hasHref = 'href' in response;
if (char1 !== '{' && transferStopped) {
if (!hasName && transferStopped) {
transferStopped = false;
return;
}
if (char1 === '|') {
var secondpos = response.indexOf('|', 1);
id = response.substr(1, secondpos - 1);
response = response.substr(secondpos + 1);
char1 = response.charAt(0);
let id;
if (hasHref) {
id = response.href;
response = response.data;
}
// Hide possible transfer dialog box
$('#dialog_box').hide();
var entry, curMeta;
switch (char1) {
case '{':
let entry, curMeta, svgStr, imgStr;
const type = hasName
? 'meta'
: response.charAt(0);
switch (type) {
case 'meta': {
// Metadata
transferStopped = false;
curMeta = JSON.parse(response);
curMeta = response;
pending[curMeta.id] = curMeta;
var name = (curMeta.name || 'file');
const name = (curMeta.name || 'file');
var message = uiStrings.notification.retrieving.replace('%s', name);
const message = uiStrings.notification.retrieving.replace('%s', name);
if (mode !== 'm') {
$.process_cancel(message, function () {
@@ -130,24 +134,26 @@ svgEditor.addExtension('imagelib', function () {
}
return;
}
case '<':
svgStr = true;
break;
case 'd':
if (response.indexOf('data:image/svg+xml') === 0) {
var pre = 'data:image/svg+xml;base64,';
var src = response.substring(pre.length);
case 'd': {
if (response.startsWith('data:image/svg+xml')) {
const pre = 'data:image/svg+xml;base64,';
const src = response.substring(pre.length);
response = svgedit.utilities.decode64(src);
svgStr = true;
break;
} else if (response.indexOf('data:image/') === 0) {
} else if (response.startsWith('data:image/')) {
imgStr = true;
break;
}
// Else fall through
}
// Else fall through
default:
// TODO: See if there's a way to base64 encode the binary data stream
// var str = 'data:;base64,' + svgedit.utilities.encode64(response, true);
// const str = 'data:;base64,' + svgedit.utilities.encode64(response, true);
// Assume it's raw image data
// importImage(str);
@@ -181,14 +187,14 @@ svgEditor.addExtension('imagelib', function () {
case 'm':
// Import multiple
multiArr.push([(svgStr ? 'svg' : 'img'), response]);
var title;
curMeta = pending[id];
let title;
if (svgStr) {
if (curMeta && curMeta.name) {
title = curMeta.name;
} else {
// Try to find a title
var xml = new DOMParser().parseFromString(response, 'text/xml').documentElement;
const xml = new DOMParser().parseFromString(response, 'text/xml').documentElement;
title = $(xml).children('title').first().text() || '(SVG #' + response.length + ')';
}
if (curMeta) {
@@ -260,8 +266,8 @@ svgEditor.addExtension('imagelib', function () {
.appendTo('#imgbrowse')
.on('click touchend', function () {
$.each(multiArr, function (i) {
var type = this[0];
var data = this[1];
const type = this[0];
const data = this[1];
if (type === 'svg') {
svgCanvas.importSvgString(data);
} else {
@@ -284,25 +290,25 @@ svgEditor.addExtension('imagelib', function () {
}
function showBrowser () {
var browser = $('#imgbrowse');
let browser = $('#imgbrowse');
if (!browser.length) {
$('<div id=imgbrowse_holder><div id=imgbrowse class=toolbar_button>' +
'</div></div>').insertAfter('#svg_docprops');
browser = $('#imgbrowse');
var allLibs = uiStrings.imagelib.select_lib;
const allLibs = uiStrings.imagelib.select_lib;
var libOpts = $('<ul id=imglib_opts>').appendTo(browser);
var frame = $('<iframe/>').prependTo(browser).hide().wrap('<div id=lib_framewrap>');
const libOpts = $('<ul id=imglib_opts>').appendTo(browser);
const frame = $('<iframe/>').prependTo(browser).hide().wrap('<div id=lib_framewrap>');
var header = $('<h1>').prependTo(browser).text(allLibs).css({
const header = $('<h1>').prependTo(browser).text(allLibs).css({
position: 'absolute',
top: 0,
left: 0,
width: '100%'
});
var cancel = $('<button>' + uiStrings.common.cancel + '</button>')
const cancel = $('<button>' + uiStrings.common.cancel + '</button>')
.appendTo(browser)
.on('click touchend', function () {
$('#imgbrowse_holder').hide();
@@ -312,9 +318,9 @@ svgEditor.addExtension('imagelib', function () {
right: -10
});
var leftBlock = $('<span>').css({position: 'absolute', top: 5, left: 10}).appendTo(browser);
const leftBlock = $('<span>').css({position: 'absolute', top: 5, left: 10}).appendTo(browser);
var back = $('<button hidden>' + uiStrings.imagelib.show_list + '</button>')
const back = $('<button hidden>' + uiStrings.imagelib.show_list + '</button>')
.appendTo(leftBlock)
.on('click touchend', function () {
frame.attr('src', 'about:blank').hide();
@@ -325,7 +331,7 @@ svgEditor.addExtension('imagelib', function () {
'margin-right': 5
}).hide();
/* var type = */ $('<select><option value=s>' +
/* const type = */ $('<select><option value=s>' +
uiStrings.imagelib.import_single + '</option><option value=m>' +
uiStrings.imagelib.import_multi + '</option><option value=o>' +
uiStrings.imagelib.open + '</option></select>').appendTo(leftBlock).change(function () {
@@ -372,10 +378,10 @@ svgEditor.addExtension('imagelib', function () {
position: 4,
title: 'Image library',
events: {
'mouseup': showBrowser
mouseup: showBrowser
}
}],
callback: function () {
callback () {
$('<style>').text(
'#imgbrowse_holder {' +
'position: absolute;' +