Skip to content

Commit

Permalink
Renamed: func names in helper. Added: new core methods. #5684
Browse files Browse the repository at this point in the history
  • Loading branch information
mrpiotr-dev committed May 9, 2019
1 parent b216f27 commit 379d7d0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 61 deletions.
41 changes: 11 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions src/core.js
Expand Up @@ -18,6 +18,7 @@ import {
objectEach
} from './helpers/object';
import { arrayFlatten, arrayMap, arrayEach, arrayReduce } from './helpers/array';
import { instanceToHTML } from './utils/parseTable';
import { getPlugin } from './plugins';
import { getRenderer } from './renderers';
import { getValidator } from './validators';
Expand Down Expand Up @@ -3555,6 +3556,26 @@ export default function Core(rootElement, userSettings, rootInstanceSymbol = fal
return getTranslatedPhrase(priv.settings.language, dictionaryKey, extraArguments);
};

/**
* Converts instance into outerHTML of HTMLTableElement.
*
* @memberof Core#
* @function toString
* @since 7.1.0
* @returns {HTMLTableElement}
*/
this.toString = () => instanceToHTML(this);

/**
* Converts instance into outerHTML of HTMLTableElement.
*
* @memberof Core#
* @function toHTML
* @since 7.1.0
* @returns {HTMLTableElement}
*/
this.toHTML = () => instanceToHTML(this);

this.timeouts = [];

/**
Expand Down
16 changes: 16 additions & 0 deletions src/index.js
Expand Up @@ -173,4 +173,20 @@ Handsontable.languages.registerLanguageDictionary = registerLanguageDictionary;
// Alias to `getTranslatedPhrase` function, for more information check it API.
Handsontable.languages.getTranslatedPhrase = (...args) => getTranslatedPhrase(...args);

/**
* Prepares Handsontable configuration based on HTMLElement.
*
* @param {Node} element Node element to prepare Handsontable configuration.
* @returns {DefaultSettings} Object with Handsontable configuration.
*/
Handsontable.fromHTML = element => Handsontable.helper.tableToSettings(element);

/**
* Prepares Handsontable configuration based on string.
*
* @param {String} htmlText Node element to prepare Handsontable configuration.
* @returns {DefaultSettings} Object with Handsontable configuration.
*/
Handsontable.fromString = htmlText => Handsontable.helper.tableToSettings(htmlText);

export default Handsontable;
14 changes: 7 additions & 7 deletions src/plugins/copyPaste/copyPaste.js
Expand Up @@ -9,7 +9,7 @@ import copyItem from './contextMenuItem/copy';
import cutItem from './contextMenuItem/cut';
import PasteEvent from './pasteEvent';
import { createElement, destroyElement } from './focusableElement';
import { arrayToTable, tableToHandsontable } from './../../utils/parseTable';
import { arrayToHTML, tableToSettings } from './../../utils/parseTable';

import './copyPaste.css';

Expand Down Expand Up @@ -411,7 +411,7 @@ class CopyPaste extends BasePlugin {
const textPlain = SheetClip.stringify(rangedData);

if (event && event.clipboardData) {
const textHTML = arrayToTable(rangedData, this.hot.rootDocument);
const textHTML = arrayToHTML(rangedData, this.hot.rootDocument);

event.clipboardData.setData('text/plain', textPlain);
event.clipboardData.setData('text/html', textHTML);
Expand Down Expand Up @@ -449,7 +449,7 @@ class CopyPaste extends BasePlugin {
const textPlain = SheetClip.stringify(rangedData);

if (event && event.clipboardData) {
const textHTML = arrayToTable(rangedData, this.hot.rootDocument);
const textHTML = arrayToHTML(rangedData, this.hot.rootDocument);

event.clipboardData.setData('text/plain', textPlain);
event.clipboardData.setData('text/html', textHTML);
Expand Down Expand Up @@ -481,13 +481,13 @@ class CopyPaste extends BasePlugin {
}

let pastedData;
let parsedConfig;
let parsedConfig = {};

if (event && typeof event.clipboardData !== 'undefined') {
const textHTML = event.clipboardData.getData('text/html');

if (textHTML && /(<table)|(<TABLE)/g.test(textHTML)) {
parsedConfig = tableToHandsontable(textHTML, this.hot.rootDocument);
parsedConfig = tableToSettings(textHTML, this.hot.rootDocument);
pastedData = parsedConfig.data;
} else {
pastedData = event.clipboardData.getData('text/plain');
Expand All @@ -503,7 +503,7 @@ class CopyPaste extends BasePlugin {
return;
}

if (this.hot.runHooks('beforePaste', inputArray, this.copyableRanges) === false) {
if (this.hot.runHooks('beforePaste', inputArray, this.copyableRanges, parsedConfig) === false) {
return;
}

Expand All @@ -516,7 +516,7 @@ class CopyPaste extends BasePlugin {
Math.min(this.hot.countCols() - 1, endColumn),
);

this.hot.runHooks('afterPaste', inputArray, this.copyableRanges, parsedConfig || {});
this.hot.runHooks('afterPaste', inputArray, this.copyableRanges, parsedConfig);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/copyPaste/test/copyPaste.e2e.js
Expand Up @@ -600,10 +600,10 @@ describe('CopyPaste', () => {
await sleep(60);

expect(beforePasteSpy.calls.count()).toEqual(1);
expect(beforePasteSpy).toHaveBeenCalledWith([['Kia']], [{ startRow: 0, startCol: 0, endRow: 0, endCol: 0 }], void 0, void 0, void 0, void 0);
expect(beforePasteSpy).toHaveBeenCalledWith([['Kia']], [{ startRow: 0, startCol: 0, endRow: 0, endCol: 0 }], {}, void 0, void 0, void 0);

expect(afterPasteSpy.calls.count()).toEqual(1);
expect(afterPasteSpy).toHaveBeenCalledWith([['Kia']], [{ startRow: 0, startCol: 0, endRow: 0, endCol: 0 }], void 0, void 0, void 0, void 0);
expect(afterPasteSpy).toHaveBeenCalledWith([['Kia']], [{ startRow: 0, startCol: 0, endRow: 0, endCol: 0 }], {}, void 0, void 0, void 0);
});

it('should be possible to block pasting', async() => {
Expand Down
45 changes: 23 additions & 22 deletions src/utils/parseTable.js
@@ -1,10 +1,22 @@
import { isEmpty } from './../helpers/mixed';

/**
* Converts Handsontable into HTMLTableElement
* @param {Handsontable} instance
* Verifies if node is an HTMLTable element.
*
* @param {Node} element Node to verify if it's an HTMLTable.
*/
export function convertToHTMLTable({ instance }) {
function isHTMLTable(element) {
return (element && element.nodeName || '').toLowerCase() === 'table';
}

/**
* Converts Handsontable into HTMLTableElement.
*
* @param {Core} instance
*
* @returns {String} outerHTML of the HTMLTableElement
*/
export function instanceToHTML(instance) {
const doc = instance.rootDocument;
const hasColumnHeaders = instance.hasColHeaders();
const hasRowHeaders = instance.hasRowHeaders();
Expand Down Expand Up @@ -72,11 +84,15 @@ export function convertToHTMLTable({ instance }) {
}

/**
* Converts javascript array into HTMLTable.
* Converts 2D array into HTMLTableElement.
*
* @param {Array} input Input array which will be converted to HTMLTable
* @param {Document} [rootDocument]
*
* @returns {String} outerHTML of the HTMLTableElement
*/
export function arrayToTable(input, rootDocument) {
// eslint-disable-next-line no-restricted-globals
export function arrayToHTML(input, rootDocument = document) {
const inputLen = input.length;
const result = ['<style>br{mso-data-placement: same-cell}</style>', '<table>'];

Expand All @@ -99,12 +115,7 @@ export function arrayToTable(input, rootDocument) {
'' :
cellData.toString().replace(/(<br(\s*|\/)>(\r\n|\n)?|\r\n|\n)/g, '<br>\r\n').replace(/\x20/gi, '&nbsp;').replace(/\t/gi, '&#9;');

// tempElement.innerText = `${isEmpty(rowData[column]) ? '' : rowData[column]}`;

columnsResult.push(`<td>${parsedCellData}</td>`);

// columnsResult.push(`<td>${tempElement.innerHTML.replace(/<br>/g, '\r\n')}</td>`);
// columnsResult.push(`<td style="white-space: normal">${tempElement.innerHTML.replace(/<br>/g, '<br>\r\n')}</td>`);
}

result.push('<tr>', ...columnsResult, '</tr>');
Expand All @@ -114,29 +125,19 @@ export function arrayToTable(input, rootDocument) {
}
}

// rootDocument.documentElement.removeChild(tempElement);

result.push('</table>');

return result.join('');
}

/**
* Helper to verify if DOM element is an HTMLTable element.
*
* @param {Element} element Node element to verify if it's an HTMLTable.
*/
function isHTMLTable(element) {
return (element && element.nodeName || '').toLowerCase() === 'table';
}

/**
* Converts HTMLTable or string into Handsontable configuration object.
*
* @param {Element|String} element Node element or string, which should contain `<table>...</table>`.
* @param {Document} [rootDocument]
*/
// eslint-disable-next-line no-restricted-globals
export function tableToHandsontable(element, rootDocument = document) {
export function tableToSettings(element, rootDocument = document) {
const settingsObj = {};
const fragment = rootDocument.createDocumentFragment();
const tempElem = rootDocument.createElement('div');
Expand Down

0 comments on commit 379d7d0

Please sign in to comment.