Skip to content

Commit 16dad6a

Browse files
committed
feat(PropTypes): add excel editor
1 parent cfd204b commit 16dad6a

File tree

4 files changed

+405
-12
lines changed

4 files changed

+405
-12
lines changed

front-end/h5/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"vue-matomo": "^3.13.0-2",
3737
"vue-quill-editor": "^3.0.6",
3838
"vue-router": "^3.0.3",
39-
"vuex": "^3.0.1"
39+
"vuex": "^3.0.1",
40+
"x-data-spreadsheet": "^1.1.4"
4041
},
4142
"devDependencies": {
4243
"@vue/cli-plugin-babel": "^3.8.0",
@@ -54,6 +55,7 @@
5455
"dart-sass": "^1.23.7",
5556
"eslint": "^5.16.0",
5657
"eslint-plugin-vue": "^5.0.0",
58+
"less-loader": "^6.1.1",
5759
"sass": "^1.18.0",
5860
"sass-loader": "^7.1.0",
5961
"stylus": "^0.54.5",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import Spreadsheet from 'x-data-spreadsheet'
2+
3+
/**
4+
*
5+
declare module ExcelRows {
6+
export interface cell {
7+
text: string;
8+
}
9+
export interface Cells {
10+
0: cell;
11+
1: cell;
12+
2: cell;
13+
}
14+
export interface ExcelRows {
15+
cells: Cells;
16+
}
17+
}
18+
*/
19+
20+
/**
21+
*
22+
BinaryMatrix = [
23+
[any, any, any, ...],
24+
[any, any, any, ...],
25+
[any, any, any, ...],
26+
]
27+
28+
ExcelDataType = [
29+
{
30+
cells: {
31+
0: { text: any },
32+
1: { text: any },
33+
2: { text: any }
34+
}
35+
},
36+
{
37+
cells: {
38+
0: { text: any },
39+
1: { text: any },
40+
2: { text: any }
41+
}
42+
},
43+
]
44+
*/
45+
46+
class Parser {
47+
/**
48+
*
49+
* @param {*} dataset ExcelDataType
50+
*/
51+
static dataset2excel (dataset) {
52+
return dataset.map(item => ({
53+
cells: {
54+
0: { text: item.x },
55+
1: { text: item.y },
56+
2: { text: item.s }
57+
}
58+
}))
59+
}
60+
61+
/**
62+
*
63+
[
64+
[1,2,3,4],
65+
[5,6,7,8],
66+
[9,10,11,12]
67+
]
68+
* @param {Object} BinaryMatrix
69+
* @returns {Object} ExcelDataType
70+
*/
71+
static binaryMatrix2excel (binaryMatrix) {
72+
const excelData = binaryMatrix.map((row, rowIndex) => {
73+
// cells: {
74+
// 0: { text: item.x },
75+
// 1: { text: item.y },
76+
// 2: { text: item.s }
77+
// }
78+
const cells = {}
79+
row.forEach((cellValue, cellIndex) => {
80+
cells[cellIndex] = { text: cellValue }
81+
})
82+
return { cells }
83+
})
84+
return excelData
85+
}
86+
87+
static excel2chartDataSet (excelData) {
88+
const rowsArray = Object.values(excelData.rows).filter(item => typeof item === 'object')
89+
const dataset = rowsArray.map(row => {
90+
const [x, y, s] = Object.values(row.cells).map(item => item.text)
91+
return {
92+
x: x,
93+
y: y,
94+
s: s
95+
}
96+
})
97+
return dataset
98+
}
99+
100+
static excel2BinaryMatrix (excelData) {
101+
const rowsArray = Object.values(excelData.rows).filter(item => typeof item === 'object')
102+
const dataset = rowsArray.map(row => {
103+
// [1,2,3,4]
104+
const cells = Object.values(row.cells).map(item => item.text)
105+
return cells
106+
})
107+
console.log('dataset', dataset)
108+
return dataset
109+
}
110+
}
111+
112+
// const getDefaultTableMatrix = () => [
113+
// [1, 2, 3, 4],
114+
// [5, 6, 7, 8],
115+
// [9, 10, 11, 12]
116+
// ]
117+
118+
export default {
119+
name: 'lbs-excel-editor',
120+
props: {
121+
value: {
122+
type: Array,
123+
// default: () => getDefaultTableMatrix()
124+
default: () => []
125+
},
126+
formatter: {
127+
type: Function,
128+
default: Parser.excel2BinaryMatrix
129+
}
130+
},
131+
computed: {
132+
innerItems: {
133+
get () {
134+
return Parser.binaryMatrix2excel(this.value)
135+
},
136+
set (val) {
137+
this.$emit('input', val)
138+
}
139+
}
140+
},
141+
render () {
142+
return <div id="excel-wrapper" ref="excel" style="margin-right: 12px;width: 100%;overflow: scroll"></div>
143+
},
144+
mounted () {
145+
const ele = this.$refs.excel
146+
const options = {
147+
showToolbar: false,
148+
showGrid: true,
149+
showContextmenu: true
150+
// view: {
151+
// height: () => 400,
152+
// width: () => ele.getBoundingClientRect().width
153+
// }
154+
}
155+
new Spreadsheet(ele, options)
156+
.loadData({
157+
rows: this.innerItems
158+
}) // load data
159+
.change(excelData => {
160+
// console.log('----------')
161+
// console.log(excelData)
162+
// console.log(this.formatter(excelData))
163+
// console.log('----------')
164+
this.$emit('change', this.formatter(excelData) /** BinaryMatrix */)
165+
// save data to db
166+
})
167+
}
168+
}

front-end/h5/src/components/core/support/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ import Vue from 'vue'
1313
import PropMultiTextItemsEditor from './prop-multi-items-editor/text.js'
1414
import ImageGallery from './image-gallery/gallery.js'
1515
import VideoGallery from './video-gallery/gallery.js'
16+
import LbsExcelEditor from './excel'
1617
import LbpTextAlign from '@luban-h5/lbs-text-align'
1718

1819
Vue.component(PropMultiTextItemsEditor.name, PropMultiTextItemsEditor)
1920
Vue.component(ImageGallery.name, ImageGallery)
2021
Vue.component(VideoGallery.name, VideoGallery)
2122
Vue.component('lbs-text-align', LbpTextAlign)
23+
Vue.component(LbsExcelEditor.name, LbsExcelEditor)

0 commit comments

Comments
 (0)