Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix: paste rich format (#824)

YuHongc7ad419da1c0
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index 043b7277a3..e50e191d22 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -433,8 +433,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
// other hooks will be executed only when the paste type is the same as the hook name, including the default one
const filteredHooks: ISheetClipboardHook[] = hooks.filter(
(h) =>
- (h.specialPasteInfo && pasteType === h.hookName) ||
- (!h.specialPasteInfo && pasteType === PREDEFINED_HOOK_NAME.DEFAULT_PASTE)
+ (!h.specialPasteInfo && h.hookName !== PREDEFINED_HOOK_NAME.DEFAULT_PASTE) || pasteType === h.hookName
);
filteredHooks.forEach((h) => {
if (rowProperties) {
diff --git a/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts b/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts
index 8cda4189fc..2f875e52fe 100644
--- a/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts
+++ b/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts
@@ -27,7 +27,7 @@ import type {
} from '../type';
import { extractNodeStyle } from './parse-node-style';
import type { IAfterProcessRule } from './paste-plugins/type';
-import parseToDom from './utils';
+import parseToDom, { generateParagraphs } from './utils';
export interface IStyleRule {
filter: string | string[] | ((node: HTMLElement) => boolean);
@@ -134,6 +134,7 @@ export class HtmlToUSMService {
body: {
dataStream: cellDataStream,
textRuns: cellTextRuns,
+ paragraphs: generateParagraphs(cellDataStream),
},
});
valueMatrix.setValue(valueMatrix.getLength(), 0, {
@@ -167,8 +168,14 @@ export class HtmlToUSMService {
}
} else {
if (dataStream) {
+ const singleDataStream = `${dataStream}\r\n`;
+ const singleDocBody: IDocumentBody = {
+ dataStream: singleDataStream,
+ textRuns,
+ paragraphs: generateParagraphs(singleDataStream),
+ };
const p = this._generateDocumentDataModelSnapshot({
- body: newDocBody,
+ body: singleDocBody,
});
valueMatrix.setValue(0, 0, {
v: dataStream,
diff --git a/packages/sheets-ui/src/services/clipboard/html-to-usm/utils.ts b/packages/sheets-ui/src/services/clipboard/html-to-usm/utils.ts
index 364bded50e..513ea4e7ff 100644
--- a/packages/sheets-ui/src/services/clipboard/html-to-usm/utils.ts
+++ b/packages/sheets-ui/src/services/clipboard/html-to-usm/utils.ts
@@ -14,7 +14,8 @@
* limitations under the License.
*/
-import type { IParagraphStyle, Nullable } from '@univerjs/core';
+import type { IParagraph, IParagraphStyle, Nullable } from '@univerjs/core';
+import { DataStreamTreeTokenType, Tools } from '@univerjs/core';
export default function parseToDom(rawHtml: string) {
const parser = new DOMParser();
@@ -64,3 +65,33 @@ export function ptToPixel(pt: number) {
return pt / PX_TO_PT_RATIO;
}
+
+export function generateParagraphs(dataStream: string, prevParagraph?: IParagraph): IParagraph[] {
+ const paragraphs: IParagraph[] = [];
+
+ for (let i = 0, len = dataStream.length; i < len; i++) {
+ const char = dataStream[i];
+
+ if (char !== DataStreamTreeTokenType.PARAGRAPH) {
+ continue;
+ }
+
+ paragraphs.push({
+ startIndex: i,
+ });
+ }
+
+ if (prevParagraph) {
+ for (const paragraph of paragraphs) {
+ if (prevParagraph.bullet) {
+ paragraph.bullet = Tools.deepClone(prevParagraph.bullet);
+ }
+
+ if (prevParagraph.paragraphStyle) {
+ paragraph.paragraphStyle = Tools.deepClone(prevParagraph.paragraphStyle);
+ }
+ }
+ }
+
+ return paragraphs;
+}