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 plain text error (#812)

Ran Luoc2b9a3421bf7
diff --git a/packages/docs/src/commands/commands/clipboard.inner.command.ts b/packages/docs/src/commands/commands/clipboard.inner.command.ts
index 29c1d9b738..64e567d1c8 100644
--- a/packages/docs/src/commands/commands/clipboard.inner.command.ts
+++ b/packages/docs/src/commands/commands/clipboard.inner.command.ts
@@ -46,7 +46,9 @@ export interface IInnerPasteCommandParams {
// Actually, the command is to handle paste event.
export const InnerPasteCommand: ICommand<IInnerPasteCommandParams> = {
id: 'doc.command.inner-paste',
+
type: CommandType.COMMAND,
+
handler: async (accessor, params: IInnerPasteCommandParams) => {
const { segmentId, body, textRanges } = params;
const undoRedoService = accessor.get(IUndoRedoService);
@@ -81,18 +83,18 @@ export const InnerPasteCommand: ICommand<IInnerPasteCommandParams> = {
const len = startOffset - memoryCursor.cursor;
if (collapsed) {
- doMutation.params!.mutations.push({
+ doMutation.params.mutations.push({
t: 'r',
len,
segmentId,
});
} else {
- doMutation.params!.mutations.push(
+ doMutation.params.mutations.push(
...getRetainAndDeleteFromReplace(selection, segmentId, memoryCursor.cursor)
);
}
- doMutation.params!.mutations.push({
+ doMutation.params.mutations.push({
t: 'i',
body,
len: body.dataStream.length,
diff --git a/packages/docs/src/services/clipboard/clipboard.service.ts b/packages/docs/src/services/clipboard/clipboard.service.ts
index 3a347e9f79..1973b177ae 100644
--- a/packages/docs/src/services/clipboard/clipboard.service.ts
+++ b/packages/docs/src/services/clipboard/clipboard.service.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { IDocumentBody } from '@univerjs/core';
+import type { IDocumentBody, IParagraph } from '@univerjs/core';
import { Disposable, IUniverInstanceService, toDisposable } from '@univerjs/core';
import { HTML_CLIPBOARD_MIME_TYPE, IClipboardInterfaceService, PLAIN_TEXT_CLIPBOARD_MIME_TYPE } from '@univerjs/ui';
import type { IDisposable } from '@wendellhu/redi';
@@ -29,6 +29,23 @@ import { UDMToHtmlService } from './udm-to-html/convertor';
HtmlToUDMService.use(PastePluginWord);
HtmlToUDMService.use(PastePluginLark);
+function generateBody(text: string): IDocumentBody {
+ // Convert all \n to \r, because we use \r to indicate paragraph break.
+ const dataStream = text.replace(/\n/g, '\r');
+ const paragraphs: IParagraph[] = [];
+
+ for (let i = 0; i < dataStream.length; i++) {
+ if (dataStream[i] === '\r') {
+ paragraphs.push({ startIndex: i });
+ }
+ }
+
+ return {
+ dataStream,
+ paragraphs,
+ };
+}
+
export interface IClipboardPropertyItem {}
export interface IDocClipboardHook {
@@ -80,10 +97,7 @@ export class DocClipboardService extends Disposable implements IDocClipboardServ
}
if (!html) {
- // TODO: @JOCS, Parsing paragraphs and sections
- return {
- dataStream: text,
- };
+ return generateBody(text);
}
const copyId = extractId(html);
diff --git a/packages/docs/src/services/clipboard/html-to-udm/converter.ts b/packages/docs/src/services/clipboard/html-to-udm/converter.ts
index 4f681621ea..48efe03c99 100644
--- a/packages/docs/src/services/clipboard/html-to-udm/converter.ts
+++ b/packages/docs/src/services/clipboard/html-to-udm/converter.ts
@@ -39,24 +39,24 @@ function matchFilter(node: HTMLElement, filter: IStyleRule['filter']) {
* Support plug-in, add custom rules,
*/
export class HtmlToUDMService {
- private static pluginList: IPastePlugin[] = [];
+ private static _pluginList: IPastePlugin[] = [];
static use(plugin: IPastePlugin) {
- if (this.pluginList.includes(plugin)) {
+ if (this._pluginList.includes(plugin)) {
throw new Error(`Univer paste plugin ${plugin.name} already added`);
}
- this.pluginList.push(plugin);
+ this._pluginList.push(plugin);
}
- private styleCache: Map<ChildNode, ITextStyle> = new Map();
+ private _styleCache: Map<ChildNode, ITextStyle> = new Map();
- private styleRules: IStyleRule[] = [];
+ private _styleRules: IStyleRule[] = [];
- private afterProcessRules: IAfterProcessRule[] = [];
+ private _afterProcessRules: IAfterProcessRule[] = [];
convert(html: string): IDocumentBody {
- const pastePlugin = HtmlToUDMService.pluginList.find((plugin) => plugin.checkPasteType(html));
+ const pastePlugin = HtmlToUDMService._pluginList.find((plugin) => plugin.checkPasteType(html));
const dom = parseToDom(html);
@@ -66,28 +66,28 @@ export class HtmlToUDMService {
};
if (pastePlugin) {
- this.styleRules = [...pastePlugin.stylesRules];
- this.afterProcessRules = [...pastePlugin.afterProcessRules];
+ this._styleRules = [...pastePlugin.stylesRules];
+ this._afterProcessRules = [...pastePlugin.afterProcessRules];
}
- this.styleCache.clear();
- this.process(null, dom?.childNodes!, newDocBody);
- this.styleCache.clear();
- this.styleRules = [];
- this.afterProcessRules = [];
+ this._styleCache.clear();
+ this._process(null, dom?.childNodes!, newDocBody);
+ this._styleCache.clear();
+ this._styleRules = [];
+ this._afterProcessRules = [];
return newDocBody;
}
- private process(parent: Nullable<ChildNode>, nodes: NodeListOf<ChildNode>, doc: IDocumentBody) {
+ private _process(parent: Nullable<ChildNode>, nodes: NodeListOf<ChildNode>, doc: IDocumentBody) {
for (const node of nodes) {
if (node.nodeType === Node.TEXT_NODE) {
// TODO: @JOCS, More characters need to be replaced, like `\b`
const text = node.nodeValue?.replace(/[\r\n]/g, '');
let style;
- if (parent && this.styleCache.has(parent)) {
- style = this.styleCache.get(parent);
+ if (parent && this._styleCache.has(parent)) {
+ style = this._styleCache.get(parent);
}
doc.dataStream += text;
@@ -100,19 +100,19 @@ export class HtmlToUDMService {
});
}
} 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));
+ const parentStyles = parent ? this._styleCache.get(parent) : {};
+ const styleRule = this._styleRules.find(({ filter }) => matchFilter(node as HTMLElement, filter));
const nodeStyles = styleRule
? styleRule.getStyle(node as HTMLElement)
: extractNodeStyle(node as HTMLElement);
- this.styleCache.set(node, { ...parentStyles, ...nodeStyles });
+ this._styleCache.set(node, { ...parentStyles, ...nodeStyles });
const { childNodes } = node;
- this.process(node, childNodes, doc);
+ this._process(node, childNodes, doc);
- const afterProcessRule = this.afterProcessRules.find(({ filter }) =>
+ const afterProcessRule = this._afterProcessRules.find(({ filter }) =>
matchFilter(node as HTMLElement, filter)
);