Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix some paste bugs #2612

Merged
merged 10 commits into from
Jun 26, 2024
Merged
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export { PermissionService } from './services/permission/permission.service';
export { AuthzIoLocalService } from './services/authz-io/authz-io-local.service';
export { IAuthzIoService } from './services/authz-io/type';
export { createDefaultUser } from './services/user-manager/const';
export { skipParseTagNames } from './types/const/clipboard';

installShims();

17 changes: 17 additions & 0 deletions packages/core/src/types/const/clipboard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export const skipParseTagNames = ['script', 'style', 'meta', 'comment', 'link'];
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { IDocumentBody, ITextStyle, Nullable } from '@univerjs/core';
import { type IDocumentBody, type ITextStyle, type Nullable, skipParseTagNames } from '@univerjs/core';

import { extractNodeStyle } from './parse-node-style';
import parseToDom from './parse-to-dom';
Expand Down Expand Up @@ -82,6 +82,10 @@ export class HtmlToUDMService {
private _process(parent: Nullable<ChildNode>, nodes: NodeListOf<ChildNode>, doc: IDocumentBody) {
for (const node of nodes) {
if (node.nodeType === Node.TEXT_NODE) {
if (node.nodeValue?.trim() === '') {
continue;
}

// TODO: @JOCS, More characters need to be replaced, like `\b`
const text = node.nodeValue?.replace(/[\r\n]/g, '');
let style;
Expand All @@ -99,6 +103,8 @@ export class HtmlToUDMService {
ts: style,
});
}
} else if (skipParseTagNames.includes(node.nodeName.toLowerCase())) {
continue;
} else if (node.nodeType === Node.ELEMENT_NODE) {
const parentStyles = parent ? this._styleCache.get(parent) : {};
const styleRule = this._styleRules.find(({ filter }) => matchFilter(node as HTMLElement, filter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function extractNodeStyle(node: HTMLElement): ITextStyle {
case 'font-weight': {
const MIDDLE_FONT_WEIGHT = 400;

if (Number(cssValue) > MIDDLE_FONT_WEIGHT) {
if (Number(cssValue) > MIDDLE_FONT_WEIGHT || String(cssValue) === 'bold') {
docStyles.bl = BooleanNumber.TRUE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const wordPastePlugin: IPastePlugin = {
afterProcessRules: [
{
filter(el: HTMLElement) {
return el.tagName === 'P' && /mso/i.test(el.className);
return el.tagName === 'P';
},
handler(doc, el) {
if (doc.paragraphs == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export interface IDocumentLayoutObject {
fill?: Nullable<string>;
}

const DEFAULT_PADDING_DATA = {
export const DEFAULT_PADDING_DATA = {
t: 0,
b: 1,
l: 2,
Expand Down
1 change: 1 addition & 0 deletions packages/engine-render/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ export { DocumentSkeleton } from './components/docs/layout/doc-skeleton';
export { ThinEngine } from './thin-engine';
export { getCharSpaceApply, getNumberUnitValue } from './components/docs/layout/tools';
export { type IChangeObserverConfig } from './scene.transformer';
export { DEFAULT_PADDING_DATA } from './components/sheets/sheet-skeleton';
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,12 @@ export function getSetCellFormulaMutations(
}

// If there is rich text, remove the style
const richText = getCellRichText(value);
if (richText) {
valueObject.p = null;
valueObject.v = richText;
if (copyInfo.pasteType === PREDEFINED_HOOK_NAME.SPECIAL_PASTE_FORMULA && value.p) {
const richText = getCellRichText(value);
if (richText) {
valueObject.p = null;
valueObject.v = richText;
}
}

valueMatrix.setValue(range.rows[row], range.cols[col], valueObject);
Expand Down
30 changes: 25 additions & 5 deletions packages/sheets-ui/src/controllers/clipboard/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { ICellData, IDocumentBody, IMutationInfo, IParagraph, IRange, Nullable } from '@univerjs/core';
import { cellToRange, IUniverInstanceService, ObjectMatrix, Range, Rectangle, Tools } from '@univerjs/core';
import { cellToRange, DEFAULT_STYLES, IUniverInstanceService, ObjectMatrix, Range, Rectangle, Tools } from '@univerjs/core';
import type {
IAddWorksheetMergeMutationParams,
IMoveRangeMutationParams,
Expand All @@ -40,6 +40,7 @@ import {
import type { IAccessor } from '@wendellhu/redi';

import numfmt from '@univerjs/engine-numfmt';
import { DEFAULT_PADDING_DATA } from '@univerjs/engine-render';
import type { ICellDataWithSpanInfo, ICopyPastePayload, ISheetDiscreteRangeLocation } from '../../services/clipboard/type';
import { COPY_TYPE } from '../../services/clipboard/type';
import { discreteRangeToRange, type IDiscreteRange, virtualizeDiscreteRanges } from '../utils/range-tools';
Expand Down Expand Up @@ -291,19 +292,20 @@ export function getSetCellValueMutations(
const valueMatrix = new ObjectMatrix<ICellData>();

matrix.forValue((row, col, value) => {
let originNumberValue;
if (!value.p && value.v && !pasteFrom) {
const content = String(value.v);
const numfmtValue = numfmt.parseDate(content) || numfmt.parseTime(content) || numfmt.parseNumber(content);
if (numfmtValue?.v && typeof numfmtValue.v === 'number') {
value.v = numfmtValue.v;
originNumberValue = numfmtValue.v;
}
}
const { row: realRow, col: realCol } = mapFunc(row, col);

if (value.p?.body) {
valueMatrix.setValue(realRow, realCol, Tools.deepClone({ p: value.p, v: value.v }));
valueMatrix.setValue(realRow, realCol, Tools.deepClone({ p: value.p, v: originNumberValue ?? value.v }));
} else {
valueMatrix.setValue(realRow, realCol, Tools.deepClone({ v: value.v }));
valueMatrix.setValue(realRow, realCol, Tools.deepClone({ v: originNumberValue ?? value.v }));
}
});
// set cell value and style
Expand Down Expand Up @@ -349,11 +351,29 @@ export function getSetCellStyleMutations(

matrix.forValue((row, col, value) => {
const newValue: ICellData = {
s: value.s,
s: Object.assign({}, {
...DEFAULT_STYLES,
pd: DEFAULT_PADDING_DATA,
bg: null,
}, value.s),
};
if (withRichFormat && value.p?.body) {
newValue.p = value.p;
}
const content = String(value.v);
const numfmtValue = numfmt.parseDate(content) || numfmt.parseTime(content) || numfmt.parseNumber(content);
ybzky marked this conversation as resolved.
Show resolved Hide resolved
if (numfmtValue?.z) {
if (!newValue.s) {
newValue.s = {};
}
if (typeof newValue.s === 'object') {
if (!newValue.s?.n) {
newValue.s.n = { pattern: numfmtValue.z };
} else {
newValue.s.n.pattern = numfmtValue.z;
}
}
}
const { row: actualRow, col: actualCol } = mapFunc(row, col);
valueMatrix.setValue(actualRow, actualCol, newValue);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,45 @@ describe('Test clipboard', () => {
expect(worksheet.getMergeData().length).toBe(3);
expect(getValues(2, 2, 2, 2)?.[0]?.[0]?.v).toEqual('Univer');
expect(getStyles(2, 2, 2, 2)?.[0]?.[0]).toStrictEqual({
bl: 1, cl: { rgb: 'black' }, ff: '等线', fs: 12, ht: 0, it: 1, vt: 2, tb: 1,
bl: 1,
cl: {
rgb: 'rgb(0,0,0)',
},
ff: '等线',
fs: 12,
ht: 0,
it: 1,
ol: {
cl: {
rgb: 'rgb(0,0,0)',
},
s: 0,
},
pd: {
b: 1,
l: 2,
r: 2,
t: 0,
},
st: {
cl: {
rgb: 'rgb(0,0,0)',
},
s: 0,
},
tb: 1,
td: 0,
tr: {
a: 0,
v: 0,
},
ul: {
cl: {
rgb: 'rgb(0,0,0)',
},
s: 0,
},
vt: 2,
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,47 @@ describe('Test clipboard', () => {
expect(res).toBeTruthy();
expect(worksheet.getMergeData().length).toBe(3);
expect(getValues(2, 2, 2, 2)?.[0]?.[0]?.v).toEqual('Univer');
expect(getStyles(2, 2, 2, 2)?.[0]?.[0]).toStrictEqual({ vt: 3, bl: 1, it: 1 });
expect(getStyles(2, 2, 2, 2)?.[0]?.[0]).toStrictEqual({
bl: 1,
cl: {
rgb: '#000',
},
ff: 'Arial',
fs: 11,
ht: 0,
it: 1,
ol: {
cl: {
rgb: '#000',
},
s: 0,
},
pd: {
b: 1,
l: 2,
r: 2,
t: 0,
},
st: {
cl: {
rgb: '#000',
},
s: 0,
},
tb: 0,
td: 0,
tr: {
a: 0,
v: 0,
},
ul: {
cl: {
rgb: '#000',
},
s: 0,
},
vt: 3,
});
});

it('test style with paste rich text style', async () => {
Expand Down
Loading
Loading