Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
feat(hook): add
Browse files Browse the repository at this point in the history
1.Add hook:workbookCreateAfter 2。update FAQ
  • Loading branch information
mengshukeji committed Dec 15, 2020
1 parent 669ed2a commit 2c6b1c2
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 40 deletions.
15 changes: 15 additions & 0 deletions docs/guide/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,19 @@ If directly developed locally:

In this case, after Luckysheet is modified in real time, the changes can be seen in the Vue project

------------

## **<span style="font-size:20px;">Q</span>** Error reporting `Store.createChart` when creating chart?

**<span style="font-size:20px;">A</span>** You need to introduce a chart plugin to use it. You should configure the chart plugin to use when the workbook is initialized. Refer to

- Plugins configuration [plugins](/guide/config.html#plugins)
- 或 官方demo [/src/index.html](https://github.com/mengshukeji/Luckysheet/blob/master/src/index.html)

------------

## **<span style="font-size:20px;">Q</span>** Can cells add custom attributes?

**<span style="font-size:20px;">A</span>** The custom attributes directly assigned to the cell object will be filtered. To make the custom attributes take effect, you need to edit the code to remove the filter attributes.

------------
17 changes: 16 additions & 1 deletion docs/zh/guide/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ Luckysheet教程里采用的CDN链接是 [jsdelivr](https://www.jsdelivr.com/pac

------------

## **<span style="font-size:20px;">Q</span>**怎样在vue工程里对Luckysheet进行二次开发`
## **<span style="font-size:20px;">Q</span>** 怎样在vue工程里对Luckysheet进行二次开发?

**<span style="font-size:20px;">A</span>** [luckysheet-vue](https://github.com/mengshukeji/luckysheet-vue) 案例是提供一个应用集成的方案。

Expand All @@ -247,4 +247,19 @@ Luckysheet教程里采用的CDN链接是 [jsdelivr](https://www.jsdelivr.com/pac

这样的话,Luckysheet实时修改后,Vue工程里是可以看到更改的

------------

## **<span style="font-size:20px;">Q</span>** 创建图表时候报错`Store.createChart`

**<span style="font-size:20px;">A</span>** 需要引入图表插件才能使用,工作簿初始化的时候应该配置图表插件使用,参考

- 插件配置 [plugins](/zh/guide/config.html#配置项)
- 或 官方demo [/src/index.html](https://github.com/mengshukeji/Luckysheet/blob/master/src/index.html)

------------

## **<span style="font-size:20px;">Q</span>** 单元格能增加自定义属性吗?

**<span style="font-size:20px;">A</span>** 直接赋值到单元格对象上的自定义属性会被过滤,要想使得自定义属性生效,需要二开去除过滤属性的代码。

------------
22 changes: 19 additions & 3 deletions src/controllers/listener.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
/**
* Monitor special variables
*/
import {createProxy} from '../utils/util'
import Store from '../store/index'
import {createProxy} from '../utils/util';
import Store from '../store/index';
import method from '../global/method';
import { getluckysheetfile } from '../methods/get'
import { toJson } from '../global/api';

const initListener = function(){
createProxy(Store,['jfredo']);
// createProxy(Store,['jfredo']);
createProxy(Store, 'jfredo',(target, property, val, receiver)=>{
if (property !== 'length') {
// 钩子函数
method.createHookFunction('updated',val)
}
} );

createProxy(Store, 'asyncLoad', (target, property, val, receiver)=>{
if(property === 'length' && val === 0){
method.createHookFunction('workbookCreateAfter', toJson())
}
})
}

export {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/menuButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -4540,7 +4540,7 @@ const menuButton = {
document.fonts && document.fonts.ready.then(function() {
// Any operation that needs to be done only after all the fonts
// have finished loading can go here.
console.log("font ready");
// console.log("font ready");
});
}

Expand Down
28 changes: 20 additions & 8 deletions src/controllers/sheetmanage.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import rhchInit from '../global/rhchInit';
import editor from '../global/editor';
import { luckysheetextendtable, luckysheetdeletetable } from '../global/extend';
import { isRealNum } from '../global/validate';
import { replaceHtml, getObjType, chatatABC } from '../utils/util';
import { replaceHtml, getObjType, chatatABC, arrayRemoveItem } from '../utils/util';
import { sheetHTML,luckysheetlodingHTML } from './constant';
import server from './server';
import luckysheetConfigsetting from './luckysheetConfigsetting';
Expand Down Expand Up @@ -758,6 +758,15 @@ const sheetmanage = {
colwidth = c2 + 1;
}

//钩子函数 表格创建之前触发
if(typeof luckysheetConfigsetting.beforeCreateDom == "function" ){
luckysheetConfigsetting.beforeCreateDom(luckysheet);
}

if(typeof luckysheetConfigsetting.workbookCreateBefore == "function"){
luckysheetConfigsetting.workbookCreateBefore(luckysheet);
}

// Store.flowdata = data;

luckysheetcreatedom(colwidth, rowheight, data, menu, title);
Expand Down Expand Up @@ -829,14 +838,17 @@ const sheetmanage = {
}
}

//钩子函数 表格创建之前触发
if(typeof luckysheetConfigsetting.beforeCreateDom == "function" ){
luckysheetConfigsetting.beforeCreateDom(luckysheet);
}
// 此处已经渲染完成表格,应该挪到前面
// //钩子函数 表格创建之前触发
// if(typeof luckysheetConfigsetting.beforeCreateDom == "function" ){
// luckysheetConfigsetting.beforeCreateDom(luckysheet);
// }

if(typeof luckysheetConfigsetting.workbookCreateBefore == "function"){
luckysheetConfigsetting.workbookCreateBefore(luckysheet);
}
// if(typeof luckysheetConfigsetting.workbookCreateBefore == "function"){
// luckysheetConfigsetting.workbookCreateBefore(luckysheet);
// }

arrayRemoveItem(Store.asyncLoad,'core');

if(luckysheetConfigsetting.pointEdit){
setTimeout(function(){
Expand Down
3 changes: 3 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ luckysheet.create = function (setting) {

if (Store.lang === 'zh') flatpickr.localize(Mandarin.zh);

// Store the currently used plugins for monitoring asynchronous loading
Store.asyncLoad.push(...luckysheetConfigsetting.plugins);

// Register plugins
initPlugins(extendsetting.plugins , extendsetting.data);

Expand Down
7 changes: 5 additions & 2 deletions src/expendPlugins/chart/plugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { seriesLoadScripts, loadLinks, $$ } from '../../utils/util'
import { seriesLoadScripts, loadLinks, $$, arrayRemoveItem } from '../../utils/util'
import { generateRandomKey, replaceHtml } from '../../utils/chartUtil'
import { getdatabyselection, getcellvalue } from '../../global/getdata';
import chartInfo from '../../store'
Expand Down Expand Up @@ -84,7 +84,7 @@ function chart(data, isDemo) {
chartInfo.chartparam.getChartJson = chartmix.default.getChartJson
chartInfo.chartparam.insertToStore = chartmix.default.insertToStore

// 初始化渲染图表
// Initialize the rendering chart
for (let i = 0; i < data.length; i++) {
// if (data[i].status == '1') {
renderCharts(data[i].chart, isDemo)
Expand All @@ -97,6 +97,9 @@ function chart(data, isDemo) {
}
}

// After the chart is loaded, mark it
arrayRemoveItem(chartInfo.asyncLoad,'chart');

});
}

Expand Down
8 changes: 4 additions & 4 deletions src/global/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,8 @@ function luckysheetDrawgridRowTitle(scrollHeight, drawHeight, offsetTop) {
//清除canvas左上角区域 防止列标题栏序列号溢出显示
// luckysheetTableContent.clearRect(0, 0, Store.rowHeaderWidth , Store.columnHeaderHeight );

luckysheetTableContent.restore();

luckysheetTableContent.restore();
luckysheetTableContent.restore();

}

function luckysheetDrawgridColumnTitle(scrollWidth, drawWidth, offsetLeft) {
Expand Down Expand Up @@ -410,7 +409,7 @@ function luckysheetDrawgridColumnTitle(scrollWidth, drawWidth, offsetLeft) {
// luckysheetTableContent.clearRect(0, 0, Store.rowHeaderWidth , Store.columnHeaderHeight );

luckysheetTableContent.restore();
luckysheetTableContent.restore();

}

function luckysheetDrawMain(scrollWidth, scrollHeight, drawWidth, drawHeight, offsetLeft, offsetTop, columnOffsetCell, rowOffsetCell, mycanvas) {
Expand Down Expand Up @@ -1073,6 +1072,7 @@ function luckysheetDrawMain(scrollWidth, scrollHeight, drawWidth, drawHeight, of
Store.measureTextCellInfoCache = {};
Store.cellOverflowMapCache = {};
}, 2000);

}


Expand Down
5 changes: 4 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
// console.info(data,sheetFile,ctx)
},
updated:function(operate){
// console.info(operate)
console.info(operate)
},
cellUpdateBefore:function(r,c,value,isRefresh){
// console.info('cellUpdateBefore',r,c,value,isRefresh)
Expand Down Expand Up @@ -162,6 +162,9 @@
cellEditBefore:function(range ){
// console.info(range)
},
workbookCreateAfter:function(json){
// console.info(json)
},


},
Expand Down
5 changes: 4 additions & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ const Store = {
allDataColumnlen:[],//列宽发生过改变的列
merge_range:{},//合并时单元格信息
checkoutData:[],//切换表格页时所需数据
}
},

// Resources that currently need to be loaded asynchronously, especially plugins. 'Core' marks the core rendering process.
asyncLoad:['core'],

}

Expand Down
72 changes: 53 additions & 19 deletions src/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { isdatatype, isdatatypemulti } from '../global/datecontroll';
import { hasChinaword,isRealNum } from '../global/validate';
import Store from '../store';
import locale from '../locale/locale';
import method from '../global/method';
// import method from '../global/method';

/**
* Common tool methods
Expand Down Expand Up @@ -788,37 +788,50 @@ function openSelfModel(id, isshowMask=true){
* 监控对象变更
* @param {*} data
*/
const createProxy = (data,list=[]) => {
if (typeof data === 'object' && data.toString() === '[object Object]') {
for (let k in data) {
if(list.includes(k)){
if (typeof data[k] === 'object') {
defineObjectReactive(data, k, data[k])
} else {
defineBasicReactive(data, k, data[k])
}
// const createProxy = (data,list=[]) => {
// if (typeof data === 'object' && data.toString() === '[object Object]') {
// for (let k in data) {
// if(list.includes(k)){
// if (typeof data[k] === 'object') {
// defineObjectReactive(data, k, data[k])
// } else {
// defineBasicReactive(data, k, data[k])
// }
// }
// }
// }
// }

const createProxy = (data, k, callback) => {
if(!data.hasOwnProperty(k)){
console.info('No %s in data',k);
return;
};

if (getObjType(data) === 'object') {
if (getObjType(data[k]) === 'object' || getObjType(data[k]) === 'array') {
defineObjectReactive(data, k, data[k], callback)
} else {
defineBasicReactive(data, k, data[k], callback)
}
}
}
}

function defineObjectReactive(obj, key, value) {
function defineObjectReactive(obj, key, value, callback) {
// 递归
// createProxy(value)
obj[key] = new Proxy(value, {
set(target, property, val, receiver) {
if (property !== 'length') {

setTimeout(() => {
// 钩子函数
method.createHookFunction('updated',val)
callback(target, property, val, receiver);
}, 0);
}

return Reflect.set(target, property, val, receiver)
}
})
}

function defineBasicReactive(obj, key, value) {
function defineBasicReactive(obj, key, value, callback) {
Object.defineProperty(obj, key, {
enumerable: true,
configurable: false,
Expand All @@ -828,11 +841,31 @@ function defineBasicReactive(obj, key, value) {
set(newValue) {
if (value === newValue) return
console.log(`发现 ${key} 属性 ${value} -> ${newValue}`)

setTimeout(() => {
callback(value,newValue);
}, 0);

value = newValue

}
})
}

/**
* Remove an item in the specified array
* @param {array} array Target array
* @param {string} item What needs to be removed
*/
function arrayRemoveItem(array, item) {
array.some((curr, index, arr)=>{
if(curr === item){
arr.splice(index, 1);
return curr === item;
}
})
}


export {
isJsonString,
Expand Down Expand Up @@ -861,5 +894,6 @@ export {
luckysheetContainerFocus,
transformRangeToAbsolute,
openSelfModel,
createProxy
createProxy,
arrayRemoveItem
}

0 comments on commit 2c6b1c2

Please sign in to comment.