fix: excel图片位置问题

This commit is contained in:
liyulin 2024-03-21 20:38:03 +08:00
parent ea9a870825
commit a8ce4c7b18
15 changed files with 438 additions and 8 deletions

View File

@ -1,7 +1,7 @@
{
"name": "@js-preview/excel",
"type" :"module",
"version": "1.7.1",
"version": "1.7.2",
"description": "",
"main": "lib/index.js",
"files": [

View File

@ -1,6 +1,6 @@
{
"name": "@vue-office/excel",
"version": "1.7.1",
"version": "1.7.2",
"description": "",
"main": "lib/index.js",
"files": [

View File

@ -23,7 +23,7 @@ let devicePixelRatio = window.devicePixelRatio;
function calcPosition(sheet, range, offset, options){
let {widthOffset, heightOffset} = options;
let {tl={}, br={}} = range;
let {nativeCol, nativeColOff, nativeRow, nativeRowOff} = tl;
let {nativeCol=0, nativeColOff=0, nativeRow=0, nativeRowOff=0} = tl;
let basicX = clipWidth;
let basicY = clipHeight;
@ -39,10 +39,10 @@ function calcPosition(sheet, range, offset, options){
let y = basicY + nativeRowOff/12700;
let {
nativeCol: nativeColEnd,
nativeColOff: nativeColOffEnd,
nativeRow: nativeRowEnd,
nativeRowOff: nativeRowOffEnd
nativeCol: nativeColEnd=0,
nativeColOff: nativeColOffEnd=0,
nativeRow: nativeRowEnd=0,
nativeRowOff: nativeRowOffEnd=0
} = br;
let width;
if(nativeCol === nativeColEnd){

View File

@ -0,0 +1 @@
import{d as r}from"./docx-53fc2607.js";import{d as p}from"./url-de9b02cf.js";import{d as x,_ as l,r as _,o as h,w as v,a as m,c as g,b as D,e as w,f as y,g as O,u as B}from"./index-75869108.js";import{P as R,u}from"./PreviewWrapper-99bcc9b3.js";import"./_commonjs-dynamic-modules-302442b1.js";typeof window.setImmediate>"u"&&(window.setImmediate=function(e,...o){setTimeout(()=>e(o))});const $=x({name:"VueOfficeDocx",props:{src:[String,ArrayBuffer,Blob],requestOptions:{type:Object,default:()=>({})},options:{type:Object,default:()=>({})}},emits:["rendered","error"],setup(e,{emit:o}){const n=_(null);let c=null;function a(){let t=n.value;r.getData(e.src,e.requestOptions).then(async i=>{c=await r.getBlob(i),r.render(c,t,e.options).then(()=>{o("rendered")}).catch(f=>{r.render("",t,e.options),o("error",f)})}).catch(i=>{r.render("",t,e.options),o("error",i)})}h(()=>{e.src&&a()}),v(()=>e.src,()=>{e.src?a():r.render("",n.value,e.options).then(()=>{o("rendered")})});function s(t){p(t||`vue-office-docx-${new Date().getTime()}.docx`,c)}return{rootRef:n,save:s}}}),b={class:"vue-office-docx"},V={class:"vue-office-docx-main",ref:"rootRef"};function k(e,o,n,c,a,s){return m(),g("div",b,[D("div",V,null,512)])}const d=l($,[["render",k]]);d.install=function(e){e.component(d.name,d)};const q={__name:"DocxDemo",setup(e){function o(){u.hideLoading()}function n(t){console.log("出差",t),u.hideLoading()}const c=location.origin+(location.pathname+"/").replace("//","/")+"static/test-files/test.docx",a=_(),s={headers:{"component-name":"VueOfficeDocx"}};return(t,i)=>(m(),w(R,{accept:".docx",placeholder:"请输入docx文件地址","default-src":c},{default:y(f=>[O(B(d),{ref_key:"docxRef",ref:a,src:f.src,"request-options":s,style:{flex:"1",height:"0"},onRendered:o,onError:n},null,8,["src"])]),_:1}))}},N=l(q,[["__scopeId","data-v-844ea5ef"]]);export{N as default};

View File

@ -0,0 +1 @@
import{l as C,r as R,S as B,a as x,g,b as $,t as L,c as k}from"./hack-62af20e5.js";import{d as q}from"./url-de9b02cf.js";import{d as F,_ as V,r as w,o as W,n as j,h as I,w as M,a as T,c as N,b as A,i as H,e as P,f as U,j as z,g as G,u as J}from"./index-75869108.js";import{P as K,u as O}from"./PreviewWrapper-99bcc9b3.js";import"./_commonjs-dynamic-modules-302442b1.js";const E={xls:!1,minColLength:20},Q=F({name:"VueOfficeExcel",props:{src:[String,ArrayBuffer,Blob],requestOptions:{type:Object,default:()=>({})},options:{type:Object,default:()=>({...E})}},emits:["rendered","error"],setup(e,{emit:l}){const i=w(null),c=w(null);let a={_worksheets:[]},n=[],f=0,o=null,t=null,u=null,h=null;function v(d){h=d,$(d,e.options.xls).then(s=>{if(!s._worksheets||s._worksheets.length===0)throw new Error("未获取到数据,可能文件格式不正确或文件已损坏");e.options.beforeTransformData&&typeof e.options.beforeTransformData=="function"&&(s=e.options.beforeTransformData(s));let{workbookData:m,medias:p,workbookSource:r}=L(s,{...E,...e.options});e.options.transformData&&typeof e.options.transformData=="function"&&(m=e.options.transformData(m)),n=p,a=r,u=null,f=0,k(),t.loadData(m),x(o,n,a._worksheets[f],u,e.options),l("rendered")}).catch(s=>{console.warn(s),n=[],a={_worksheets:[]},k(),t&&t.loadData({}),l("error",s)})}const D=C.debounce(R,200).bind(this,c.value),b=new MutationObserver(D),y={attributes:!0,childList:!0,subtree:!0};W(()=>{j(()=>{b.observe(c.value,y),D(c),t=new B(c.value,{mode:"read",showToolbar:!1,showContextmenu:e.options.showContextmenu||!1,view:{height:()=>i.value&&i.value.clientHeight||300,width:()=>i.value&&i.value.clientWidth||1200},row:{height:24,len:100},col:{len:26,width:80,indexWidth:60,minWidth:60},autoFocus:!1}).loadData({});let d=t.bottombar.swapFunc;t.bottombar.swapFunc=function(r){d.call(t.bottombar,r),f=r,setTimeout(()=>{t.reRender(),x(o,n,a._worksheets[f],u,e.options)})};let s=t.sheet.editor.clear;t.sheet.editor.clear=function(...r){s.apply(t.sheet.editor,r),setTimeout(()=>{x(o,n,a._worksheets[f],u,e.options)})};let m=t.sheet.editor.setOffset;t.sheet.editor.setOffset=function(...r){m.apply(t.sheet.editor,r),u=r[0],x(o,n,a._worksheets[f],u,e.options)},o=c.value.querySelector("canvas").getContext("2d"),e.src&&g(e.src,e.requestOptions).then(v).catch(r=>{n=[],a={_worksheets:[]},t.loadData({}),l("error",r)})})}),I(()=>{b.disconnect(),t=null}),M(()=>e.src,()=>{e.src?g(e.src,e.requestOptions).then(v).catch(d=>{n=[],a={_worksheets:[]},t.loadData({}),l("error",d)}):(n=[],a={_worksheets:[]},t.loadData({}),l("error",new Error("src属性不能为空")))});function S(d){q(d||`vue-office-excel-${new Date().getTime()}.xlsx`,h)}return{wrapperRef:i,rootRef:c,save:S}}}),X={class:"vue-office-excel",ref:"wrapperRef"},Y={class:"vue-office-excel-main",ref:"rootRef"};function Z(e,l,i,c,a,n){return T(),N("div",X,[A("div",Y,null,512)],512)}const _=V(Q,[["render",Z]]);_.install=function(e){e.component(_.name,_)};const re={__name:"ExcelDemo",setup(e){function l(){O.hideLoading()}function i(o){console.log("出差",o),O.hideLoading()}function c(o){return console.log("beforeTransformData",o),o}function a(o){return console.log("transformData",o),o}const n=location.origin+(location.pathname+"/").replace("//","/")+"static/test-files/test.xlsx",f=w();return(o,t)=>{const u=H("loading");return T(),P(K,{accept:".xlsx,.xls",placeholder:"请输入xlsx文件地址","default-src":n},{default:U(h=>[z(G(J(_),{ref_key:"docxRef",ref:f,src:h.src,options:{beforeTransformData:c,transformData:a,xls:h.xls},style:{flex:"1",height:"0"},onRendered:l,onError:i},null,8,["src","options"]),[[u,!0]])]),_:1})}}};export{re as default};

View File

@ -0,0 +1 @@
var p=Object.defineProperty;var c=(i,e,t)=>e in i?p(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var n=(i,e,t)=>(c(i,typeof e!="symbol"?e+"":e,t),t);import{d as r}from"./docx-53fc2607.js";import{d as l}from"./url-de9b02cf.js";import{r as h,o as d,a as u,c as m}from"./index-75869108.js";import"./_commonjs-dynamic-modules-302442b1.js";typeof window.setImmediate>"u"&&(window.setImmediate=function(i,...e){setTimeout(()=>i(e))});class w{constructor(e,t={},s={}){n(this,"container",null);n(this,"wrapper",null);n(this,"wrapperMain",null);n(this,"options",{});n(this,"requestOptions",{});n(this,"fileData",null);this.container=e,this.options=t,this.requestOptions=s,this.createWrapper()}createWrapper(){this.wrapper=document.createElement("div"),this.wrapper.className="vue-office-docx",this.wrapperMain=document.createElement("div"),this.wrapperMain.className="vue-office-docx-main",this.wrapper.appendChild(this.wrapperMain),this.container.appendChild(this.wrapper)}setOptions(e){this.options=e}setRequestOptions(e){this.requestOptions=e}preview(e){return new Promise((t,s)=>{r.getData(e,this.requestOptions).then(async o=>{this.fileData=await r.getBlob(o),r.render(this.fileData,this.wrapperMain,this.options).then(()=>{t()}).catch(a=>{r.render("",this.wrapperMain,this.options),s(a)})}).catch(o=>{r.render("",this.wrapperMain,this.options),s(o)})})}save(e){l(e||`js-preview-docx-${new Date().getTime()}.docx`,this.fileData)}destroy(){this.container.removeChild(this.wrapper),this.container=null,this.wrapper=null,this.wrapperMain=null,this.options=null,this.requestOptions=null}}function f(i,e,t){return new w(i,e,t)}const v={init:f};const q={__name:"JsDocxDemo",setup(i){const e=h(null);return d(()=>{let t=v.init(e.value);t.preview("/vue-office/examples/dist/static/test-files/test.docx").then(s=>{console.log("docx preview done"),setTimeout(()=>{t.preview("/vue-office/examples/dist/static/test-files/test2.docx")},3e3)}).catch(s=>{console.log("err",s)})}),(t,s)=>(u(),m("div",{ref_key:"dom",ref:e},null,512))}};export{q as default};

View File

@ -0,0 +1 @@
var c=Object.defineProperty;var p=(o,e,t)=>e in o?c(o,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[e]=t;var s=(o,e,t)=>(p(o,typeof e!="symbol"?e+"":e,t),t);import{S as u,a as h,b as d,t as w,c as l,l as f,r as m,g as x}from"./hack-62af20e5.js";import{d as b}from"./url-de9b02cf.js";/* empty css */import{r as D,o as k,a as v,c as S}from"./index-75869108.js";import"./_commonjs-dynamic-modules-302442b1.js";const _={xls:!1,minColLength:20};class E{constructor(e,t={},r={}){s(this,"container",null);s(this,"wrapper",null);s(this,"wrapperMain",null);s(this,"options",{});s(this,"requestOptions",{});s(this,"mediasSource",[]);s(this,"workbookDataSource",{_worksheets:[]});s(this,"sheetIndex",1);s(this,"ctx",null);s(this,"xs",null);s(this,"offset",null);s(this,"observer",null);s(this,"fileData",null);this.container=e,this.options={..._,...t},this.requestOptions=r,this.createWrapper(),this.initSpreadsheet(),this.hack()}createWrapper(){this.wrapper=document.createElement("div"),this.wrapper.className="vue-office-excel",this.wrapperMain=document.createElement("div"),this.wrapperMain.className="vue-office-excel-main",this.wrapper.appendChild(this.wrapperMain),this.container.appendChild(this.wrapper)}initSpreadsheet(){this.xs=new u(this.wrapperMain,{mode:"read",showToolbar:!1,showContextmenu:this.options.showContextmenu||!1,view:{height:()=>this.wrapper&&this.wrapper.clientHeight||300,width:()=>this.wrapper&&this.wrapper.clientWidth||1200},row:{height:24,len:100},col:{len:26,width:80,indexWidth:60,minWidth:60},autoFocus:!1}).loadData({});let e=this,t=this.xs.bottombar.swapFunc;this.xs.bottombar.swapFunc=function(a){t.call(e.xs.bottombar,a),e.sheetIndex=a,setTimeout(()=>{e.xs.reRender(),h(e.ctx,e.mediasSource,e.workbookDataSource._worksheets[e.sheetIndex],e.offset)})};let r=this.xs.sheet.editor.clear;this.xs.sheet.editor.clear=function(...a){r.apply(e.xs.sheet.editor,a),setTimeout(()=>{h(e.ctx,e.mediasSource,e.workbookDataSource._worksheets[e.sheetIndex],e.offset)})};let i=this.xs.sheet.editor.setOffset;this.xs.sheet.editor.setOffset=function(...a){i.apply(e.xs.sheet.editor,a),e.offset=a[0],h(e.ctx,e.mediasSource,e.workbookDataSource._worksheets[e.sheetIndex],e.offset)};const n=this.wrapperMain.querySelector("canvas");this.ctx=n.getContext("2d")}renderExcel(e){return this.fileData=e,d(e,this.options.xls).then(t=>{if(!t._worksheets||t._worksheets.length===0)throw new Error("未获取到数据,可能文件格式不正确或文件已损坏");this.options.beforeTransformData&&typeof this.options.beforeTransformData=="function"&&(t=this.options.beforeTransformData(t));let{workbookData:r,medias:i,workbookSource:n}=w(t,this.options);this.options.transformData&&typeof this.options.transformData=="function"&&(r=this.options.transformData(r)),this.mediasSource=i,this.workbookDataSource=n,this.offset=null,this.sheetIndex=0,l(),this.xs.loadData(r),h(this.ctx,this.mediasSource,this.workbookDataSource._worksheets[this.sheetIndex],this.offset)}).catch(t=>(this.mediasSource=[],this.workbookDataSource={_worksheets:[]},l(),this.xs.loadData({}),Promise.reject(t)))}hack(){const e=f.debounce(m,200).bind(this,this.wrapperMain);this.observer=new MutationObserver(e);const t={attributes:!0,childList:!0,subtree:!0};this.observer.observe(this.wrapperMain,t),e(this.wrapperMain)}setOptions(e){this.options=e}setRequestOptions(e){this.requestOptions=e}preview(e){return new Promise((t,r)=>{x(e,this.requestOptions).then(i=>{this.renderExcel(i).then(t).catch(n=>{this.mediasSource=[],this.workbookDataSource={_worksheets:[]},this.xs.loadData({}),r(n)})}).catch(i=>{this.mediasSource=[],this.workbookDataSource={_worksheets:[]},this.xs.loadData({}),r(i)})})}save(e){b(e||`js-preview-excel-${new Date().getTime()}.xlsx`,this.fileData)}destroy(){this.observer.disconnect(),this.container.removeChild(this.wrapper),this.container=null,this.wrapper=null,this.wrapperMain=null,this.ctx=null,this.xs=null,this.observer=null,this.options=null,this.requestOptions=null,this.mediasSource=null,this.workbookDataSource=null}}function g(o,e,t){return new E(o,e,t)}const O={init:g},P={__name:"JsExcelDemo",setup(o){const e=D(null);return k(()=>{window.myExcelPreview=O.init(e.value,{transformData:function(t){return console.log("transformData",t),t}}),window.myExcelPreview.preview("/vue-office/examples/dist/static/test-files/test.xlsx").then(t=>{console.log("excel preview done",window.myExcelPreview)}).catch(t=>{console.log("err",t)})}),(t,r)=>(v(),S("div",{ref_key:"dom",ref:e,style:{height:"calc(100vh - 50px)"}},null,512))}};export{P as default};

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
import{r as u,p as C,_ as I,w as N,q as n,a as d,c as k,u as t,g as i,f as r,s as g,t as S,e as x,m as y,l as V,v as A,x as R,b as T}from"./index-75869108.js";function $(e){const p=u("url"),a=u(e),o=u(e),l=u(typeof e=="string"?e.endsWith(".xls"):!1),c=u([]);function h(_){l.value=_.name.endsWith("xls");let f=new FileReader;return f.onload=s=>{let v=s.target.result;o.value=v},f.readAsArrayBuffer(_),!1}return{type:p,inputSrc:a,src:o,xls:l,fileList:c,beforeUpload:h}}let w=u(!1),B;function q(e){B=C.loading(e,0),w.value=!0}function E(){w.value===!0&&(B(),w.value=!1)}const F={loading:w,showLoading:q,hideLoading:E};function j(){return location.href.includes("test")}const z=e=>(A("data-v-0bd825a5"),e=e(),R(),e),D={class:"preview-wrapper"},G={key:0,class:"operate-area"},H=z(()=>T("div",{class:"preview-wrapper-main"},null,-1)),J={__name:"PreviewWrapper",props:{accept:String,placeholder:String,defaultSrc:String},setup(e){const p=e,{type:a,inputSrc:o,src:l,xls:c,fileList:h,beforeUpload:_}=$(p.defaultSrc);return N(l,()=>{F.showLoading()},{immediate:!0}),(f,s)=>{const v=n("a-radio-button"),L=n("a-radio-group"),U=n("a-input"),b=n("a-button"),W=n("upload-outlined"),P=n("a-upload");return d(),k("div",D,[t(j)()?y("",!0):(d(),k("div",G,[i(L,{value:t(a),"onUpdate:value":s[0]||(s[0]=m=>S(a)?a.value=m:null),"button-style":"solid"},{default:r(()=>[i(v,{value:"url"},{default:r(()=>[g("远程文件地址")]),_:1}),i(v,{value:"upload"},{default:r(()=>[g("上传本地文件")]),_:1})]),_:1},8,["value"]),t(a)==="url"?(d(),x(U,{key:0,value:t(o),"onUpdate:value":s[1]||(s[1]=m=>S(o)?o.value=m:null),placeholder:p.placeholder,style:{width:"600px","margin-left":"10px"}},null,8,["value","placeholder"])):y("",!0),t(a)==="url"?(d(),x(b,{key:1,type:"primary",style:{"margin-left":"10px"},onClick:s[2]||(s[2]=m=>{l.value=t(o),c.value=t(o).endsWith("xls")})},{default:r(()=>[g(" 预览 ")]),_:1})):y("",!0),t(a)!=="url"?(d(),x(P,{key:2,accept:p.accept,action:"",beforeUpload:t(_),"file-list":[]},{default:r(()=>[i(b,{style:{"margin-left":"10px"}},{default:r(()=>[i(W),g(" 选择文件 ")]),_:1})]),_:1},8,["accept","beforeUpload"])):y("",!0)])),V(f.$slots,"default",{src:t(l),xls:t(c)},void 0,!0),H])}}},M=I(J,[["__scopeId","data-v-0bd825a5"]]);export{M as P,F as u};

97
examples/dist/assets/docx-53fc2607.js vendored Normal file

File diff suppressed because one or more lines are too long

202
examples/dist/assets/hack-62af20e5.js vendored Normal file

File diff suppressed because one or more lines are too long

121
examples/dist/assets/index-75869108.js vendored Normal file

File diff suppressed because one or more lines are too long

1
examples/dist/assets/omit-f856690a.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -7,7 +7,7 @@
content="width=device-width, initial-scale=1.0,minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<title>@vue-office演示demo</title>
<script type="module" crossorigin src="/vue-office/examples/dist/assets/index-9ad6e9da.js"></script>
<script type="module" crossorigin src="/vue-office/examples/dist/assets/index-75869108.js"></script>
<link rel="stylesheet" href="/vue-office/examples/dist/assets/index-cc8fb346.css">
</head>
<body>