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

feat: replacement in document body #1451

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
207 changes: 207 additions & 0 deletions packages/core/src/docs/data-model/__tests__/replacement.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
73 changes: 73 additions & 0 deletions packages/core/src/docs/data-model/replacement.ts
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 3 additions & 3 deletions packages/core/src/docs/data-model/text-x/text-x.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading