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.

feat: replacement in document body (#1451)

Ran Luoe258abde82ce
diff --git a/packages/core/src/docs/data-model/__tests__/replacement.spec.ts b/packages/core/src/docs/data-model/__tests__/replacement.spec.ts
new file mode 100644
index 0000000000..697a68ffb1
--- /dev/null
+++ b/packages/core/src/docs/data-model/__tests__/replacement.spec.ts
@@ -0,0 +1,207 @@
+/**
+ * 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.
+ */
+
+import { describe, expect, it } from 'vitest';
+
+import { BooleanNumber } from '../../../types/enum/text-style';
+import { replaceInDocumentBody } from '../replacement';
+
+function getTestDocumentBody() {
+ const documentBody = {
+ dataStream: '荷塘月色\r作者:朱月色\r\n',
+ textRuns: [
+ {
+ st: 0,
+ ed: 3,
+ ts: {
+ fs: 24,
+ ff: 'Microsoft YaHei',
+ cl: {
+ rgb: 'rgb(0, 0, 0)',
+ },
+ bl: BooleanNumber.TRUE,
+ },
+ },
+ {
+ st: 3,
+ ed: 4,
+ ts: {
+ fs: 24,
+ ff: 'Microsoft YaHei',
+ cl: {
+ rgb: 'rgb(23, 23, 23)',
+ },
+ bl: BooleanNumber.TRUE,
+ },
+ },
+ {
+ st: 5,
+ ed: 11,
+ ts: {
+ fs: 18,
+ ff: 'Microsoft YaHei',
+ cl: {
+ rgb: 'rgb(30, 30, 30)',
+ },
+ bl: BooleanNumber.FALSE,
+ },
+ },
+ ],
+ paragraphs: [
+ {
+ startIndex: 4,
+ paragraphStyle: {
+ spaceAbove: 10,
+ lineSpacing: 2,
+ spaceBelow: 0,
+ },
+ },
+ {
+ startIndex: 11,
+ paragraphStyle: {
+ spaceAbove: 10,
+ lineSpacing: 2,
+ spaceBelow: 0,
+ },
+ },
+ ],
+ sectionBreaks: [
+ {
+ startIndex: 12,
+ },
+ ],
+ };
+
+ return documentBody;
+}
+
+describe('test case in replaceInDocumentBody utils', () => {
+ it('Should replace all `query` to `target`', () => {
+ const documentBody = getTestDocumentBody();
+ const expectedBody = {
+ dataStream: '荷塘Jocs\r作者:朱Jocs\r\n',
+ textRuns: [
+ {
+ st: 0,
+ ed: 6,
+ ts: {
+ fs: 24,
+ ff: 'Microsoft YaHei',
+ cl: {
+ rgb: 'rgb(0, 0, 0)',
+ },
+ bl: BooleanNumber.TRUE,
+ },
+ },
+ {
+ st: 7,
+ ed: 15,
+ ts: {
+ fs: 18,
+ ff: 'Microsoft YaHei',
+ cl: {
+ rgb: 'rgb(30, 30, 30)',
+ },
+ bl: BooleanNumber.FALSE,
+ },
+ },
+ ],
+ paragraphs: [
+ {
+ startIndex: 6,
+ paragraphStyle: {
+ spaceAbove: 10,
+ lineSpacing: 2,
+ spaceBelow: 0,
+ },
+ },
+ {
+ startIndex: 15,
+ paragraphStyle: {
+ spaceAbove: 10,
+ lineSpacing: 2,
+ spaceBelow: 0,
+ },
+ },
+ ],
+ sectionBreaks: [
+ {
+ startIndex: 16,
+ },
+ ],
+ };
+
+ expect(replaceInDocumentBody(documentBody, '月色', 'Jocs')).toEqual(expectedBody);
+ });
+
+ it('Should replace all `query` to `target` when `target` is empty', () => {
+ const documentBody = getTestDocumentBody();
+ const expectedBody = {
+ dataStream: '荷塘\r作者:朱\r\n',
+ textRuns: [
+ {
+ st: 0,
+ ed: 2,
+ ts: {
+ fs: 24,
+ ff: 'Microsoft YaHei',
+ cl: {
+ rgb: 'rgb(0, 0, 0)',
+ },
+ bl: BooleanNumber.TRUE,
+ },
+ },
+ {
+ st: 3,
+ ed: 7,
+ ts: {
+ fs: 18,
+ ff: 'Microsoft YaHei',
+ cl: {
+ rgb: 'rgb(30, 30, 30)',
+ },
+ bl: BooleanNumber.FALSE,
+ },
+ },
+ ],
+ paragraphs: [
+ {
+ startIndex: 2,
+ paragraphStyle: {
+ spaceAbove: 10,
+ lineSpacing: 2,
+ spaceBelow: 0,
+ },
+ },
+ {
+ startIndex: 7,
+ paragraphStyle: {
+ spaceAbove: 10,
+ lineSpacing: 2,
+ spaceBelow: 0,
+ },
+ },
+ ],
+ sectionBreaks: [
+ {
+ startIndex: 8,
+ },
+ ],
+ };
+
+ expect(replaceInDocumentBody(documentBody, '月色', '')).toEqual(expectedBody);
+ });
+});
diff --git a/packages/core/src/docs/data-model/replacement.ts b/packages/core/src/docs/data-model/replacement.ts
new file mode 100644
index 0000000000..7f372bac24
--- /dev/null
+++ b/packages/core/src/docs/data-model/replacement.ts
@@ -0,0 +1,73 @@
+/**
+ * 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.
+ */
+
+import type { IDocumentBody, IDocumentData } from '../../types/interfaces/i-document-data';
+import { DocumentDataModel } from './document-data-model';
+import { TextX } from './text-x/text-x';
+
+export function replaceInDocumentBody(body: IDocumentBody, query: string, target: string): IDocumentBody {
+ if (query === '') {
+ return body;
+ }
+
+ const mockDocumentData: IDocumentData = {
+ id: 'mock-id',
+ body,
+ documentStyle: {},
+ };
+
+ const documentDataModel = new DocumentDataModel(mockDocumentData);
+
+ const queryLen = query.length;
+ let index;
+
+ while ((index = documentDataModel.getBody()!.dataStream.indexOf(query)) >= 0) {
+ const textX = new TextX();
+
+ if (index > 0) {
+ textX.retain(index);
+ }
+
+ if (target.length > 0) {
+ const sliceBody = documentDataModel.sliceBody(index, index + queryLen);
+ const replaceBody: IDocumentBody = {
+ dataStream: target,
+ };
+
+ if (Array.isArray(sliceBody?.textRuns) && sliceBody.textRuns.length) {
+ replaceBody.textRuns = [{
+ ...sliceBody.textRuns[0],
+ st: 0,
+ ed: target.length,
+ }];
+ }
+
+ textX.insert(target.length, replaceBody);
+ }
+
+ textX.delete(queryLen);
+
+ const actions = textX.serialize();
+
+ documentDataModel.apply(actions);
+ }
+
+ const newBody = documentDataModel.getBody()!;
+
+ documentDataModel.dispose();
+
+ return newBody;
+}
diff --git a/packages/core/src/docs/data-model/text-x/text-x.ts b/packages/core/src/docs/data-model/text-x/text-x.ts
index e2e9da5bb6..614b7893dc 100644
--- a/packages/core/src/docs/data-model/text-x/text-x.ts
+++ b/packages/core/src/docs/data-model/text-x/text-x.ts
@@ -81,7 +81,7 @@ export class TextX {
private _actions: TextXAction[] = [];
- insert(len: number, body: IDocumentBody, segmentId: string): this {
+ insert(len: number, body: IDocumentBody, segmentId = ''): this {
const insertAction: IInsertAction = {
t: TextXActionType.INSERT,
body,
@@ -95,7 +95,7 @@ export class TextX {
return this;
}
- retain(len: number, segmentId: string, body?: IDocumentBody, coverType?: UpdateDocsAttributeType): this {
+ retain(len: number, segmentId = '', body?: IDocumentBody, coverType?: UpdateDocsAttributeType): this {
const retainAction: IRetainAction = {
t: TextXActionType.RETAIN,
len,
@@ -115,7 +115,7 @@ export class TextX {
return this;
}
- delete(len: number, segmentId: string): this {
+ delete(len: number, segmentId = ''): this {
const deleteAction: IDeleteAction = {
t: TextXActionType.DELETE,
len,
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 6b20f309b1..2d7ea47472 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -38,6 +38,7 @@ export {
export { ActionIterator } from './docs/data-model/text-x/action-iterator';
export { getBodySlice, composeBody } from './docs/data-model/text-x/utils';
export { TextX } from './docs/data-model/text-x/text-x';
+export { replaceInDocumentBody } from './docs/data-model/replacement';
export * from './observer';
export { Plugin, PluginType } from './plugin/plugin';
export {