UNPKG

svgedit

Version:

Powerful SVG-Editor for your browser

11 lines (10 loc) 3.49 kB
/** * @file ext-grid.js * * @license Apache-2.0 * * @copyright 2010 Redou Mine, 2010 Alexis Deveria * */ const loadExtensionTranslation=async function(e){let o;const r=e.configObj.pref("lang");try{o=await function __variableDynamicImportRuntime0__(e){switch(e){case"./locale/en.js":return Promise.resolve().then((function(){return t}));case"./locale/fr.js":return Promise.resolve().then((function(){return n}));case"./locale/zh-CN.js":return Promise.resolve().then((function(){return i}));default:return new Promise((function(t,n){("function"==typeof queueMicrotask?queueMicrotask:setTimeout)(n.bind(null,new Error("Unknown variable dynamic import: "+e)))}))}}("./locale/".concat(r,".js"))}catch(e){console.warn("Missing translation (".concat(r,") for ").concat("grid"," - using 'en'")),o=await Promise.resolve().then((function(){return t}))}e.i18next.addResourceBundle(r,"grid",o.default)};var e={name:"grid",async init(e){let{NS:t,getTypeMap:n}=e;const i=this;await loadExtensionTranslation(i);const{svgCanvas:o}=i,{$id:r}=o,a=r("svgcanvas").ownerDocument,{assignAttributes:s}=o,c=document.createElement("canvas"),l=r("canvasBackground"),d=n(),u=[.01,.1,1,10,100,1e3];let g=i.configObj.curConfig.showGrid||!1;c.style.display="none",i.$svgEditor.appendChild(c);const h=a.createElementNS(t.SVG,"svg");s(h,{id:"canvasGrid",width:"100%",height:"100%",x:0,y:0,overflow:"visible",display:"none"}),l.appendChild(h);const p=a.createElementNS(t.SVG,"defs"),f=a.createElementNS(t.SVG,"pattern");s(f,{id:"gridpattern",patternUnits:"userSpaceOnUse",x:0,y:0,width:100,height:100});const m=a.createElementNS(t.SVG,"image");s(m,{x:0,y:0,width:100,height:100}),f.append(m),p.append(f),r("canvasGrid").appendChild(p);const v=a.createElementNS(t.SVG,"rect");s(v,{width:"100%",height:"100%",x:0,y:0,"stroke-width":0,stroke:"none",fill:"url(#gridpattern)",style:"pointer-events: none; display:visible;"}),r("canvasGrid").appendChild(v);const updateGrid=e=>{const t=d[i.configObj.curConfig.baseUnit]*e,n=100/t;let r=1;u.some((e=>(r=e,n<=e)));const a=r*t;c.width=a,c.height=a;const s=c.getContext("2d"),l=.5,g=a/10;s.globalAlpha=.2,s.strokeStyle=i.configObj.curConfig.gridColor;for(let e=1;e<10;e++){const t=Math.round(g*e)+.5,n=0;s.moveTo(t,a),s.lineTo(t,n),s.moveTo(a,t),s.lineTo(n,t)}s.stroke(),s.beginPath(),s.globalAlpha=.5,s.moveTo(l,a),s.lineTo(l,0),s.moveTo(a,l),s.lineTo(0,l),s.stroke();const h=c.toDataURL("image/png");m.setAttribute("width",a),m.setAttribute("height",a),m.parentNode.setAttribute("width",a),m.parentNode.setAttribute("height",a),o.setHref(m,h)},gridUpdate=()=>{g&&updateGrid(o.getZoom()),r("canvasGrid").style.display=g?"block":"none",r("view_grid").pressed=g};return{name:i.i18next.t("".concat("grid",":name")),zoomChanged(e){g&&updateGrid(e)},callback(){const e=document.createElement("template"),t="".concat("grid",":buttons.0.title");e.innerHTML='\n <se-button id="view_grid" title="'.concat(t,'" src="grid.svg"></se-button>\n '),r("editor_panel").append(e.content.cloneNode(!0)),r("view_grid").addEventListener("click",(()=>{i.configObj.curConfig.showGrid=g=!g,gridUpdate()})),g&&gridUpdate()}}}},t=Object.freeze({__proto__:null,default:{name:"View Grid",buttons:[{title:"Show/Hide Grid"}]}}),n=Object.freeze({__proto__:null,default:{name:"Grille",buttons:[{title:"Afficher/Cacher Grille"}]}}),i=Object.freeze({__proto__:null,default:{name:"网格视图",buttons:[{title:"显示/隐藏网格"}]}});export{e as default}; //# sourceMappingURL=ext-grid.js.map