(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,3 @@
/* globals RGBColor, DOMParser, jsPDF */
/* eslint-disable no-var */
/*
* svgToPdf.js
*
@@ -21,10 +19,10 @@
*
*/
(function (jsPDFAPI, undef) {
'use strict';
import RGBColor from '../canvg/rgbcolor.js';
var pdfSvgAttr = {
const jsPDFAPI = jsPDF.API;
const pdfSvgAttr = {
// allowed attributes. all others are removed from the preview.
g: ['stroke', 'fill', 'stroke-width'],
line: ['x1', 'y1', 'x2', 'y2', 'stroke', 'stroke-width'],
@@ -34,19 +32,19 @@ var pdfSvgAttr = {
text: ['x', 'y', 'font-size', 'font-family', 'text-anchor', 'font-weight', 'font-style', 'fill']
};
var attributeIsNotEmpty = function (node, attr) {
var attVal = attr ? node.getAttribute(attr) : node;
const attributeIsNotEmpty = function (node, attr) {
const attVal = attr ? node.getAttribute(attr) : node;
return attVal !== '' && attVal !== null;
};
var nodeIs = function (node, possible) {
return possible.indexOf(node.tagName.toLowerCase()) > -1;
const nodeIs = function (node, possible) {
return possible.includes(node.tagName.toLowerCase());
};
var removeAttributes = function (node, attributes) {
var toRemove = [];
const removeAttributes = function (node, attributes) {
const toRemove = [];
[].forEach.call(node.attributes, function (a) {
if (attributeIsNotEmpty(a) && attributes.indexOf(a.name.toLowerCase()) === -1) {
if (attributeIsNotEmpty(a) && !attributes.includes(a.name.toLowerCase())) {
toRemove.push(a.name);
}
});
@@ -56,19 +54,19 @@ var removeAttributes = function (node, attributes) {
});
};
var svgElementToPdf = function (element, pdf, options) {
const svgElementToPdf = function (element, pdf, options) {
// pdf is a jsPDF object
// console.log("options =", options);
var remove = (options.removeInvalid === undef ? false : options.removeInvalid);
var k = (options.scale === undef ? 1.0 : options.scale);
var colorMode = null;
const remove = (options.removeInvalid === undefined ? false : options.removeInvalid);
const k = (options.scale === undefined ? 1.0 : options.scale);
let colorMode = null;
[].forEach.call(element.children, function (node) {
// console.log("passing: ", node);
var hasFillColor = false;
// var hasStrokeColor = false;
var fillRGB;
// let hasStrokeColor = false;
let hasFillColor = false;
let fillRGB;
if (nodeIs(node, ['g', 'line', 'rect', 'ellipse', 'circle', 'text'])) {
var fillColor = node.getAttribute('fill');
const fillColor = node.getAttribute('fill');
if (attributeIsNotEmpty(fillColor)) {
fillRGB = new RGBColor(fillColor);
if (fillRGB.ok) {
@@ -86,9 +84,9 @@ var svgElementToPdf = function (element, pdf, options) {
if (attributeIsNotEmpty(node, 'stroke-width')) {
pdf.setLineWidth(k * parseInt(node.getAttribute('stroke-width'), 10));
}
var strokeColor = node.getAttribute('stroke');
const strokeColor = node.getAttribute('stroke');
if (attributeIsNotEmpty(strokeColor)) {
var strokeRGB = new RGBColor(strokeColor);
const strokeRGB = new RGBColor(strokeColor);
if (strokeRGB.ok) {
// hasStrokeColor = true;
pdf.setDrawColor(strokeRGB.r, strokeRGB.g, strokeRGB.b);
@@ -160,7 +158,7 @@ var svgElementToPdf = function (element, pdf, options) {
if (hasFillColor) {
pdf.setTextColor(fillRGB.r, fillRGB.g, fillRGB.b);
}
var fontType = '';
let fontType = '';
if (node.hasAttribute('font-weight')) {
if (node.getAttribute('font-weight') === 'bold') {
fontType = 'bold';
@@ -176,13 +174,13 @@ var svgElementToPdf = function (element, pdf, options) {
}
}
pdf.setFontType(fontType);
var pdfFontSize = 16;
if (node.hasAttribute('font-size')) {
pdfFontSize = parseInt(node.getAttribute('font-size'), 10);
}
var box = node.getBBox();
const pdfFontSize = node.hasAttribute('font-size')
? parseInt(node.getAttribute('font-size'), 10)
: 16;
const box = node.getBBox();
// FIXME: use more accurate positioning!!
var x, y, xOffset = 0;
let x, y, xOffset = 0;
if (node.hasAttribute('text-anchor')) {
switch (node.getAttribute('text-anchor')) {
case 'end': xOffset = box.width; break;
@@ -213,7 +211,7 @@ var svgElementToPdf = function (element, pdf, options) {
};
jsPDFAPI.addSVG = function (element, x, y, options) {
options = (options === undef ? {} : options);
options = (options === undefined ? {} : options);
options.x_offset = x;
options.y_offset = y;
@@ -223,4 +221,3 @@ jsPDFAPI.addSVG = function (element, x, y, options) {
svgElementToPdf(element, this, options);
return this;
};
}(jsPDF.API));