カスタムメニュービュー

SpreadJSでは、組み込みのコンテキストメニューをカスタマイズできます。

定義済みのコンテキストメニュースタイルクラスを書き換えることで、異なるスタイルを適用できます。次に、例を示します。 コンテキストメニュー項目の表示構造をカスタマイズするには、MenuViewのcreateMenuItemElementメソッドを上書きします。 MenuViewのgetCommandOptionsメソッドを上書きすると、コマンドの実行時にオプションを表示できます。
import * as React from 'react'; import * as ReactDOM from 'react-dom'; import './styles.css'; import { AppFunc } from './app-func'; import { App } from './app-class'; // 1. Functional Component sample ReactDOM.render(<AppFunc />, document.getElementById('app')); // 2. Class Component sample // ReactDOM.render(<App />, document.getElementById('app'));
import * as React from 'react'; import { SpreadSheets, Worksheet } from '@mescius/spread-sheets-react'; import GC from '@mescius/spread-sheets'; import '@mescius/spread-sheets-resources-ja'; GC.Spread.Common.CultureManager.culture("ja-jp"); import './styles.css'; const useState = React.useState, colors = ['rgb(255,255,255)', 'rgb(0,255,255)', 'rgb(255,0,255)', 'rgb(255,255,0)', 'rgb(255,0,0)', 'rgb(0,255,0)', 'rgb(0,0,255)', 'rgb(0,0,0)']; function createColorpicker() { let colorPicker = document.createElement('div'); colorPicker.className = 'colorPickerContent'; for (let j = 0; j < 8; j++) { let colorDom = document.createElement("div"); colorDom.className = 'colorDom'; colorDom.style['backgroundColor'] = colors[j]; colorPicker.appendChild(colorDom); } return colorPicker; } function isMenuItemExist(menuData, menuItemName) { let i = 0, count = menuData.length; for (; i < count; i++) { if (menuItemName === menuData[i].name) { return true; } } } function createInput() { let inputBlock = document.createElement('input'); inputBlock.className = 'inputBlock'; inputBlock.type = 'text'; inputBlock.value = '1'; inputBlock.setAttribute('gcUIElement', 'gcContextMenu'); inputBlock.style = 'width: 20px'; inputBlock.onclick = function (ev) { if (ev.target) { ev.stopPropagation() } } return inputBlock; } export function AppFunc() { const [spread, setSpread] = useState(null); const initSpread = (spread) => { setSpread(spread); } const addMoreMenuItems = ($event) => { if (isMenuItemExist(spread.contextMenu.menuData, "selectColorWithBg") || isMenuItemExist(spread.contextMenu.menuData, "insertRows")) { spread.contextMenu.menuData.forEach(function (item, index) { if (item && (item.name === "selectColorWithBg" || item.name === "insertRows")) { spread.contextMenu.menuData.splice(index, 1); } }); return; } let commandManager = spread.commandManager(); // prepair select with a background color command and menu item let selectWithABackgroundColor = { text: "背景色を設定する", name: "selectColorWithBg", workArea: "viewport", subMenu: [ { name: "selectColorPicker", command: "selectWithBg" } ] }; spread.contextMenu.menuData.push(selectWithABackgroundColor); let selectWithABackgroundColorCommand = { canUndo: false, execute: function (spread, options) { if (options.commandOptions) { let style = new GC.Spread.Sheets.Style(); style.name = 'style1'; style.backColor = options.commandOptions; let sheet = spread.getActiveSheet(); sheet.suspendPaint(); let selections = sheet.getSelections(); let selectionIndex = 0, selectionCount = selections.length; for (; selectionIndex < selectionCount; selectionIndex++) { let selection = selections[selectionIndex]; for (let i = selection.row; i < (selection.row + selection.rowCount); i++) { for (let j = selection.col; j < (selection.col + selection.colCount); j++) { sheet.setStyle(i, j, style, GC.Spread.Sheets.SheetArea.viewport); } } } sheet.resumePaint(); } } }; commandManager.register("selectWithBg", selectWithABackgroundColorCommand, null, false, false, false, false); // prepair insert rows by specified row count command and menu item let insertRows = { text: "行を挿入する", name: "insertRows", command: "insertRows", workArea: "rowHeader" }; spread.contextMenu.menuData.push(insertRows); let insertRowsCommand = { canUndo: false, execute: function (spread, options) { let rowCount = parseInt(options.commandOptions); if (!isNaN(rowCount)) { let sheet = spread.getSheetFromName(options.sheetName); sheet.suspendPaint(); sheet.addRows(options.activeRow, rowCount); sheet.resumePaint(); } } }; commandManager.register("insertRows", insertRowsCommand, null, false, false, false, false); // customize context menu function CustomMenuView() { } CustomMenuView.prototype = new GC.Spread.Sheets.ContextMenu.MenuView(); CustomMenuView.prototype.createMenuItemElement = function (menuItemData) { let self = this; if (menuItemData.name === "selectColorPicker") { let containers = GC.Spread.Sheets.ContextMenu.MenuView.prototype.createMenuItemElement.call(self, menuItemData); let supMenuItemContainer = containers[0]; while (supMenuItemContainer.firstChild) { supMenuItemContainer.removeChild(supMenuItemContainer.firstChild); } let colorPicker = createColorpicker(); supMenuItemContainer.appendChild(colorPicker); return supMenuItemContainer; } else if (menuItemData.name === "insertRows") { let containers = GC.Spread.Sheets.ContextMenu.MenuView.prototype.createMenuItemElement.call(self, menuItemData); let supMenuItemContainer = containers[0]; supMenuItemContainer.appendChild(createInput()); return supMenuItemContainer; } else { let menuItemView = GC.Spread.Sheets.ContextMenu.MenuView.prototype.createMenuItemElement.call(self, menuItemData); return menuItemView; } }; CustomMenuView.prototype.getCommandOptions = function (menuItemData, host, event) { if (menuItemData && menuItemData.name === "selectColorPicker") { let ele = event.target || event.srcElement; return ele.style.backgroundColor; } else if (menuItemData && menuItemData.name === "insertRows") { return host.getElementsByClassName("inputBlock")[0].value; } }; spread.contextMenu.menuView = new CustomMenuView(); } return ( <div class="sample-tutorial"> <div class="sample-spreadsheets"> <SpreadSheets workbookInitialized={spread => initSpread(spread)}> <Worksheet> </Worksheet> </SpreadSheets> </div> <Panel addMoreMenuItems={(e) => addMoreMenuItems(e)} ></Panel> </div> ); } function CheckBoxInput(props) { const [checked, setChecked] = useState(props.checked); return ( <input type="checkbox" id={props.id} checked={checked} onChange={(e) => { setChecked(e.target.checked); props.onChange(e) }} /> ); } function Panel(props) { return ( <div class="options-container"> <div className="options-row"> 任意のセルを右クリックすると、コンテキストメニューが表示されます。コンテキストメニューは必要に応じて簡単に変更することができます。次のオプションを選択すると、セルの背景色を変更できる新しいコンテキストメニューがビューポートに追加されます。また、指定した行数を挿入できるコンテキストメニューが行ヘッダに追加されます。 </div> <div className="options-row"> <CheckBoxInput id="addMoreMenuItems" checked={false} onChange={props.addMoreMenuItems}></CheckBoxInput> <label htmlFor="addMoreMenuItems">メニュー項目を追加する</label> </div> </div> ); }
import * as React from 'react'; import { SpreadSheets, Worksheet } from '@mescius/spread-sheets-react'; import GC from '@mescius/spread-sheets'; import '@mescius/spread-sheets-resources-ja'; GC.Spread.Common.CultureManager.culture("ja-jp"); import './styles.css'; const Component = React.Component, useState = React.useState, colors = ['rgb(255,255,255)', 'rgb(0,255,255)', 'rgb(255,0,255)', 'rgb(255,255,0)', 'rgb(255,0,0)', 'rgb(0,255,0)', 'rgb(0,0,255)', 'rgb(0,0,0)']; function createColorpicker() { let colorPicker = document.createElement('div'); colorPicker.className = 'colorPickerContent'; for (let j = 0; j < 8; j++) { let colorDom = document.createElement("div"); colorDom.className = 'colorDom'; colorDom.style['backgroundColor'] = colors[j]; colorPicker.appendChild(colorDom); } return colorPicker; } function isMenuItemExist(menuData, menuItemName) { let i = 0, count = menuData.length; for (; i < count; i++) { if (menuItemName === menuData[i].name) { return true; } } } function createInput() { let inputBlock = document.createElement('input'); inputBlock.className = 'inputBlock'; inputBlock.type = 'text'; inputBlock.value = '1'; inputBlock.setAttribute('gcUIElement', 'gcContextMenu'); inputBlock.style = 'width: 20px'; inputBlock.onclick = function (ev) { if (ev.target) { ev.stopPropagation() } } return inputBlock; } export class App extends Component { constructor(props) { super(props); this.spread = null; } render() { return ( <div class="sample-tutorial"> <div class="sample-spreadsheets"> <SpreadSheets workbookInitialized={spread => this.initSpread(spread)}> <Worksheet> </Worksheet> </SpreadSheets> </div> <Panel addMoreMenuItems={(e) => this.addMoreMenuItems(e)} ></Panel> </div> ); } initSpread(spread) { this.spread = spread; } addMoreMenuItems($event) { let spread = this.spread; if (isMenuItemExist(spread.contextMenu.menuData, "selectColorWithBg") || isMenuItemExist(spread.contextMenu.menuData, "insertRows")) { spread.contextMenu.menuData.forEach(function (item, index) { if (item && (item.name === "selectColorWithBg" || item.name === "insertRows")) { spread.contextMenu.menuData.splice(index, 1); } }); return; } let commandManager = spread.commandManager(); // prepair select with a background color command and menu item let selectWithABackgroundColor = { text: "背景色を設定する", name: "selectColorWithBg", workArea: "viewport", subMenu: [ { name: "selectColorPicker", command: "selectWithBg" } ] }; spread.contextMenu.menuData.push(selectWithABackgroundColor); let selectWithABackgroundColorCommand = { canUndo: false, execute: function (spread, options) { if (options.commandOptions) { let style = new GC.Spread.Sheets.Style(); style.name = 'style1'; style.backColor = options.commandOptions; let sheet = spread.getActiveSheet(); sheet.suspendPaint(); let selections = sheet.getSelections(); let selectionIndex = 0, selectionCount = selections.length; for (; selectionIndex < selectionCount; selectionIndex++) { let selection = selections[selectionIndex]; for (let i = selection.row; i < (selection.row + selection.rowCount); i++) { for (let j = selection.col; j < (selection.col + selection.colCount); j++) { sheet.setStyle(i, j, style, GC.Spread.Sheets.SheetArea.viewport); } } } sheet.resumePaint(); } } }; commandManager.register("selectWithBg", selectWithABackgroundColorCommand, null, false, false, false, false); // prepair insert rows by specified row count command and menu item let insertRows = { text: "行を挿入する", name: "insertRows", command: "insertRows", workArea: "rowHeader" }; spread.contextMenu.menuData.push(insertRows); let insertRowsCommand = { canUndo: false, execute: function (spread, options) { let rowCount = parseInt(options.commandOptions); if (!isNaN(rowCount)) { let sheet = spread.getSheetFromName(options.sheetName); sheet.suspendPaint(); sheet.addRows(options.activeRow, rowCount); sheet.resumePaint(); } } }; commandManager.register("insertRows", insertRowsCommand, null, false, false, false, false); // customize context menu function CustomMenuView() { } CustomMenuView.prototype = new GC.Spread.Sheets.ContextMenu.MenuView(); CustomMenuView.prototype.createMenuItemElement = function (menuItemData) { let self = this; if (menuItemData.name === "selectColorPicker") { let containers = GC.Spread.Sheets.ContextMenu.MenuView.prototype.createMenuItemElement.call(self, menuItemData); let supMenuItemContainer = containers[0]; while (supMenuItemContainer.firstChild) { supMenuItemContainer.removeChild(supMenuItemContainer.firstChild); } let colorPicker = createColorpicker(); supMenuItemContainer.appendChild(colorPicker); return supMenuItemContainer; } else if (menuItemData.name === "insertRows") { let containers = GC.Spread.Sheets.ContextMenu.MenuView.prototype.createMenuItemElement.call(self, menuItemData); let supMenuItemContainer = containers[0]; supMenuItemContainer.appendChild(createInput()); return supMenuItemContainer; } else { let menuItemView = GC.Spread.Sheets.ContextMenu.MenuView.prototype.createMenuItemElement.call(self, menuItemData); return menuItemView; } }; CustomMenuView.prototype.getCommandOptions = function (menuItemData, host, event) { if (menuItemData && menuItemData.name === "selectColorPicker") { let ele = event.target || event.srcElement; return ele.style.backgroundColor; } else if (menuItemData && menuItemData.name === "insertRows") { return host.getElementsByClassName("inputBlock")[0].value; } }; spread.contextMenu.menuView = new CustomMenuView(); } } function CheckBoxInput(props) { const [checked, setChecked] = useState(props.checked); return ( <input type="checkbox" id={props.id} checked={checked} onChange={(e) => { setChecked(e.target.checked); props.onChange(e) }} /> ); } function Panel(props) { return ( <div class="options-container"> <div className="options-row"> 任意のセルを右クリックすると、コンテキストメニューが表示されます。コンテキストメニューは必要に応じて簡単に変更することができます。次のオプションを選択すると、セルの背景色を変更できる新しいコンテキストメニューがビューポートに追加されます。また、指定した行数を挿入できるコンテキストメニューが行ヘッダに追加されます。 </div> <div className="options-row"> <CheckBoxInput id="addMoreMenuItems" checked={false} onChange={props.addMoreMenuItems}></CheckBoxInput> <label htmlFor="addMoreMenuItems">メニュー項目を追加する</label> </div> </div> ); }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/ja/react/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/spread/source/data/data.js" type="text/javascript"></script> <!-- SystemJS --> <script src="$DEMOROOT$/ja/react/node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('$DEMOROOT$/ja/lib/react/license.js').then(function () { System.import('./src/app'); }); </script> </head> <body> <div id="app"></div> </body> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 280px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 280px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } input { padding: 4px 6px; } label { margin-bottom: 6px; } .options-row { font-size: 14px; padding: 5px; margin-top: 10px; } input[type=button] { margin-top: 6px; display: block; } .colorPickerContent{ width: 100%; background-color:white; } .colorDom{ width: 14px; height: 14px; margin:0 0 0 6px; display: inline-block; border: solid 1px #333333; vertical-align: top; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } #app { height: 100%; }
(function (global) { System.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true, react: true }, meta: { '*.css': { loader: 'css' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-resources-ja': 'npm:@mescius/spread-sheets-resources-ja/index.js', '@mescius/spread-sheets-react': 'npm:@mescius/spread-sheets-react/index.js', '@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js', 'react': 'npm:react/umd/react.production.min.js', 'react-dom': 'npm:react-dom/umd/react-dom.production.min.js', 'css': 'npm:systemjs-plugin-css/css.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', 'systemjs-babel-build':'npm:systemjs-plugin-babel/systemjs-babel-browser.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'jsx' }, "node_modules": { defaultExtension: 'js' }, } }); })(this);