-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add UMD module support Add Matrix property #565
Conversation
@rdfriedl can you review this please? :) |
while it dose not completely fix the bug, it dose highlights a big problem with they typings. // while this works
var matrix: svgjs.Matrix = new SVG.Matrix();
// this dose not
var matrix: svgjs.Matrix = new SVG.Matrix(1,0,0,1,0,0); i think the typings are going to need a little bit more work to fix and update them before they match. also its partly my fault that this bug is here, when i was reviewing the code i found two typos here and here |
export as namespace svgjs; | ||
|
||
declare var svgjs: svgjs.Library; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you explain what this code dose?
because when i test the typings with this code it just says that the library dose not have a default export.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason for that is that svg.js
does not contain a default
export. As a result we need to import the entire module content as an object, which is the corresponding es6 syntax. [1]
could you explain what this code dose?
For clarification:
… anywhere the module keyword was used when declaring an internal module, the namespace keyword can and should be used instead. [2]
The export as namespace
makes the svgjs
namespace available globally if you use the /// <reference
directive. [3]
The rest of the code is doing the same as before.
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Description
[2] https://www.typescriptlang.org/docs/handbook/namespaces.html
[3] microsoft/TypeScript#7125
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i did some testing with importing the library and it looks like
import SVG from 'svg.js'
and
import * as SVG from 'svg.js'
returns the default SVG()
function.
Its also possible to do
import {Element} from 'svg.js'
with that in mind I tested the old typings file and the one here and this is what i got.
The original
this worked fine
import SVG from 'svg.js'
var draw: svgjs.Doc = new SVG.Doc();
this dose not, because the original typings only exports svgjs.Library
import {Doc} from 'svg.js'
var draw: svgjs.Doc = new Doc();
this dose not work.
it makes SVG a object with the default
property set to the default SVG()
function
import * as SVG from 'svg.js'
var draw: svgjs.Doc = new SVG.Doc();
With your version
this dose not work, it says that there is no default export
import SVG from 'svg.js'
var draw: svgjs.Doc = new SVG.Doc();
but this works fine.
except for the svgjs.Doc
, it says that Namespace 'svgjs' has no exported member 'Doc'
import {Doc} from 'svg.js'
var draw: svgjs.Doc = new Doc();
also this works.
but has the same problem with the svgjs namespace
import * as SVG from 'svg.js'
var draw: svgjs.Doc = new Doc();
Conclusion
it looks like your version works a little better then the original.
we would just have to fix the default export and the namespace error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great. 🍻
we would just have to fix the default export and the namespace error.
Like you already said in the comment bellow. I will try to do some work on this issue.
export interface Matrix { | ||
(source: any): Matrix; | ||
new(source: any): Matrix; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this makes sense though i think it highlights an even bigger problem with the typings.
when i wrote the typings i forgot to supply the arguments to the methods on the library
what i mean is, this works
var matrix: svgjs.Matrix = new SVG.Matrix();
but this dose not
var element: svgjs.Element = new SVG.Element();
var matrix: svgjs.Matrix = new SVG.Matrix(element);
this is a mistake on my end since i set the Matrix
as a function on the Library
and not a property that holds a reference to the Matrix
class
a few examples, here and here
but i remembered to include in other places, like here
of course this means that we would have to run over the whole library and fix the parts like this
interface Library { Number(): void; }
and change then to something like this
interface Library { Number: Number; }
and then fix this
export interface Number{
// change this
(value: any, unit?: any): Number;
// to this, that way SVG.Number can only be called with the "new"
new (value: any, unit?: any): Number;
}
so while this dose fix it for the Matrix class i think we will end up having to go over the entire file and fix each class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good starting point to fix the typing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had a lot of free time on my hands so i decided to fix this bug.
If you want to check it out, its over here
Im not an expert in using git or github so im not sure how we would merge my code into this PR.
any ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome.
I could cherry pick your commits and include them into the PR, if this is ok for you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go right ahead, it's better then me creating a whole new PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also create a PR for his PR by forking his fork and merge it there then merge it here. However: Cherrypicking might be faster ^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice try, @Fuzzyma 😝
The commits are now included into the PR. There are no errors when importing and using the Matrix
property of the SVG
object.
I think we are ready to merge this …
there is still one problem left to fix. import * as SVG from 'svg.js';
var draw: svgjs.Doc = new SVG.Doc();
document.body.appendChild(draw.node); but this dose not work anymore import SVG from 'svg.js';
var draw: svgjs.Doc = new SVG.Doc();
document.body.appendChild(draw.node); it says that |
This is a core design decision. You can see the discussion and conclusions in this issue. |
Here is a documentation change which also address our case. |
oh i see, i guess typescript modules dont work exactly like ES6 modules 😄 @Fuzzyma can you merge this? |
sure thing. Thanks for your work! |
Matrix
propertyFix for #564