Skip to content

Commit

Permalink
fix: 修复重构工具栏配置后不传 showtoolbar showtoolbarConfig 时的bug
Browse files Browse the repository at this point in the history
  • Loading branch information
magicds committed Apr 21, 2021
1 parent 831f958 commit 4dd527b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 45 deletions.
36 changes: 21 additions & 15 deletions src/controllers/resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Store from '../store';
import locale from '../locale/locale';
import sheetmanage from './sheetmanage';
import tooltip from '../global/tooltip'
import { $$, getObjType } from "../utils/util";
import { $$, getObjType, camel2split } from "../utils/util";
import { defaultToolbar, toolbarIdMap } from './toolbar';

let gridW = 0,
Expand Down Expand Up @@ -344,18 +344,14 @@ export function menuToolBarWidth() {
* }
*/
function buildBoolBarConfig() {
const obj = {};
// 数组形式直接生成
if (getObjType(showtoolbarConfig) === 'array') {
// show 为 false
if (!showtoolbar) {
return obj;
}
let obj = {};
function array2Config(arr) {
const obj = {};
let current,next;
let index = 0;
for (let i = 0; i<showtoolbarConfig.length; i++) {
current = showtoolbarConfig[i];
next = showtoolbarConfig[i + 1];
for (let i = 0; i<arr.length; i++) {
current = arr[i];
next = arr[i + 1];
if (current !== '|') {
obj[current] = {
ele: toolbarIdMap[current],
Expand All @@ -364,14 +360,22 @@ export function menuToolBarWidth() {
}
if (next === '|') {
if (getObjType(obj[current].ele) === 'array') {
obj[current].ele.push(`#toolbar-separator-${current.toLowerCase()}`);
obj[current].ele.push(`#toolbar-separator-${camel2split(current)}`);
} else {
obj[current].ele = [obj[current].ele, `#toolbar-separator-${current.toLowerCase()}`];
obj[current].ele = [obj[current].ele, `#toolbar-separator-${camel2split(current)}`];
}
}
}
return obj;
}
// 数组形式直接生成
if (getObjType(showtoolbarConfig) === 'array') {
// show 为 false
if (!showtoolbar) {
return obj;
}
return array2Config(showtoolbarConfig);
}
// 否则为全部中从记录中挑选显示或隐藏
const config = defaultToolbar.reduce(function(total, curr) {
if (curr !== '|') {
Expand Down Expand Up @@ -405,12 +409,14 @@ export function menuToolBarWidth() {
}
if (next === '|') {
if (getObjType(obj[current].ele) === 'array') {
obj[current].ele.push(`#toolbar-separator-${current.toLowerCase()}`);
obj[current].ele.push(`#toolbar-separator-${camel2split(current)}`);
} else {
obj[current].ele = [obj[current].ele, `#toolbar-separator-${current.toLowerCase()}`];
obj[current].ele = [obj[current].ele, `#toolbar-separator-${camel2split(current)}`];
}
}
}
} else {
obj = array2Config(defaultToolbar);
}

return obj;
Expand Down
46 changes: 17 additions & 29 deletions src/controllers/toolbar.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import locale from '../locale/locale';
import luckysheetConfigsetting from './luckysheetConfigsetting';

import { getObjType } from '../utils/util';
import { getObjType, camel2split } from '../utils/util';

// 默认的工具栏按钮
export const defaultToolbar = [
Expand Down Expand Up @@ -876,36 +876,24 @@ export function createToolbarHtml() {
delete showtoolbarConfig.undoRedo;
}
Object.assign(config, showtoolbarConfig);

for (let i = 0; i < defaultToolbar.length; i++) {
let key = defaultToolbar[i];
if (!config[key]) {
// 如果当前元素隐藏 按照之前的规则 后面紧跟的 | 分割也不需要显示了
if (defaultToolbar[i + 1] === '|') {
i++;
}
return;
}
if (key === '|') {
buttonHTML.push(
`<div id="toolbar-separator-${camel2split(key)}" class="luckysheet-toolbar-separator luckysheet-inline-block" style="user-select: none;"></div>`
);
} else {
buttonHTML.push(htmlMap[key]);
}
for (let i = 0; i < defaultToolbar.length; i++) {
let key = defaultToolbar[i];
if (!config[key] && key !== '|') {
// 如果当前元素隐藏 按照之前的规则 后面紧跟的 | 分割也不需要显示了
if (defaultToolbar[i + 1] === '|') {
i++;
}
continue;
}
if (key === '|') {
buttonHTML.push(
`<div id="toolbar-separator-${camel2split(key)}" class="luckysheet-toolbar-separator luckysheet-inline-block" style="user-select: none;"></div>`
);
} else {
buttonHTML.push(htmlMap[key]);
}
}

console.log(buttonHTML);
return buttonHTML.join('');
}

/**
* camel 形式的单词转换为 - 形式 如 fillColor -> fill-color
* @param {string} camel camel 形式
* @returns
*/
function camel2split(camel) {
return camel.replace(/([A-Z])/g, function(all, group) {
return '-' + group.toLowerCase();
});
}
13 changes: 12 additions & 1 deletion src/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,16 @@ function arrayRemoveItem(array, item) {
})
}

/**
* camel 形式的单词转换为 - 形式 如 fillColor -> fill-color
* @param {string} camel camel 形式
* @returns
*/
function camel2split(camel) {
return camel.replace(/([A-Z])/g, function(all, group) {
return '-' + group.toLowerCase();
});
}

export {
isJsonString,
Expand Down Expand Up @@ -900,5 +910,6 @@ export {
transformRangeToAbsolute,
openSelfModel,
createProxy,
arrayRemoveItem
arrayRemoveItem,
camel2split
}

0 comments on commit 4dd527b

Please sign in to comment.