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.

15.7 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * 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 { Nullable } from '../../shared';
import type { ITextRangeParam } from '../../sheets/typedef';
import type { LocaleType } from '../../types/enum';
import type {
    IDocumentBody,
    IDocumentData,
    IDocumentRenderConfig,
    IDocumentStyle,
    IDrawings,
    IListData,
} from '../../types/interfaces/i-document-data';
import type { IPaddingData } from '../../types/interfaces/i-style-data';
import type { JSONXActions } from './json-x/json-x';
import { BehaviorSubject } from 'rxjs';
import { isInternalEditorID } from '../../common/const';
import { mergeWith } from '../../common/lodash';
import { UnitModel, UniverInstanceType } from '../../common/unit';
import { generateRandomId } from '../../shared/random-id';
import { Tools } from '../../shared/tools';
import { createSectionId } from '../section-break-id';
import { calculateDocumentStatistics } from './document-statistics';
import { getEmptySnapshot } from './empty-snapshot';
import { JSON1, JSONX } from './json-x/json-x';
import { PRESET_LIST_TYPE } from './preset-list-type';
import { getPlainText } from './text-x/build-utils/parse';
import { getBodySlice, SliceBodyType } from './text-x/utils';

export const DEFAULT_DOC = {
    id: 'default_doc',
    documentStyle: {},
};

export interface IDocumentStatistics {
    words: number;
    charactersWithoutSpaces: number;
    charactersWithSpaces: number;
    paragraphs: number;
    nonAsianWords: number;
    asianCharactersAndKoreanWords: number;
}

export interface IDocumentStatisticsOptions {
    locale?: LocaleType;
    ranges?: Readonly<ITextRangeParam[]>;
    signal?: AbortSignal;
}

function normalizeLegacyPageBreakSectionMetadata(body: IDocumentBody | undefined): IDocumentBody | undefined {
    if (body?.dataStream == null || body.sectionBreaks == null) {
        return body;
    }

    const compatibleSectionBreaks = body.sectionBreaks.filter(({ startIndex }) =>
        body.dataStream[startIndex] === '\n' || startIndex <= 0 || body.dataStream[startIndex - 1] !== '\f'
    );
    const existingSectionIds = new Set<string>();
    let didChange = compatibleSectionBreaks.length !== body.sectionBreaks.length;
    const sectionBreaks = compatibleSectionBreaks.map((sectionBreak) => {
        if (sectionBreak.sectionId && !existingSectionIds.has(sectionBreak.sectionId)) {
            existingSectionIds.add(sectionBreak.sectionId);
            return sectionBreak;
        }

        didChange = true;
        return { ...sectionBreak, sectionId: createSectionId(existingSectionIds) };
    });

    return didChange ? { ...body, sectionBreaks } : body;
}

function createDocumentSnapshot(snapshot: Partial<IDocumentData>): IDocumentData {
    if (snapshot.id != null && isInternalEditorID(snapshot.id)) {
        return { ...DEFAULT_DOC, ...snapshot } as IDocumentData;
    }

    const defaultSnapshot = getEmptySnapshot(snapshot.id, snapshot.locale, snapshot.title, snapshot.documentStyle?.documentFlavor);

    if (Tools.isEmptyObject(snapshot)) {
        return defaultSnapshot;
    }

    const mergedSnapshot = mergeWith({}, defaultSnapshot, snapshot, (_objValue, srcValue, key) => {
        if (Array.isArray(srcValue)) {
            return srcValue;
        }

        if (key === 'body' && srcValue != null && typeof srcValue === 'object') {
            return srcValue;
        }
    });

    // Existing snapshots with a body must preserve the absence of document
    // defaults. The renderer provides the legacy flavor fallback for them.
    if (snapshot.body != null && snapshot.documentStyle?.defaultParagraphStyle === undefined) {
        delete mergedSnapshot.documentStyle.defaultParagraphStyle;
    }

    mergedSnapshot.body = normalizeLegacyPageBreakSectionMetadata(mergedSnapshot.body);

    return mergedSnapshot;
}

interface IDrawingUpdateConfig {
    left: number;
    top: number;
    height: number;
    width: number;
}

class DocumentDataModelSimple extends UnitModel<IDocumentData, UniverInstanceType.UNIVER_DOC> {
    override type: UniverInstanceType.UNIVER_DOC = UniverInstanceType.UNIVER_DOC;

    override getUnitId(): string {
        throw new Error('Method not implemented.');
    }

    protected readonly _name$ = new BehaviorSubject<string>('');
    override name$ = this._name$.asObservable();

    protected snapshot: IDocumentData;
    private _mutationRevision = 0;

    constructor(snapshot: Partial<IDocumentData>) {
        super();

        this.snapshot = createDocumentSnapshot(snapshot);
        this._name$.next(this.snapshot.title ?? 'No Title');
    }

    override getRev(): number {
        return this.snapshot.rev ?? 1;
    }

    override incrementRev(): void {
        this.snapshot.rev = this.getRev() + 1;
    }

    override setRev(rev: number): void {
        this.snapshot.rev = rev;
    }

    setName(name: string) {
        this.snapshot.title = name;
        this._markMutation();
        this._name$.next(name);
    }

    get drawings() {
        return this.snapshot.drawings;
    }

    get documentStyle() {
        return this.snapshot.documentStyle;
    }

    get lists() {
        return this.snapshot.lists;
    }

    get zoomRatio() {
        return this.snapshot.settings?.zoomRatio || 1;
    }

    resetDrawing(drawings: IDrawings, drawingsOrder: string[]) {
        this.snapshot.drawings = drawings;
        this.snapshot.drawingsOrder = drawingsOrder;
        this._markMutation();
    }

    getBody() {
        return this.snapshot.body;
    }

    getSnapshot() {
        return this.snapshot;
    }

    getBulletPresetList() {
        const customLists = this.snapshot.lists ?? {};
        const lists: Record<string, IListData> = {
            ...PRESET_LIST_TYPE,
            ...customLists,
        };

        return lists;
    }

    updateDocumentId(unitId: string) {
        this.snapshot.id = unitId;
    }

    updateDocumentRenderConfig(config: IDocumentRenderConfig) {
        const { documentStyle } = this.snapshot;

        if (documentStyle.renderConfig == null) {
            documentStyle.renderConfig = config;
        } else {
            documentStyle.renderConfig = {
                ...documentStyle.renderConfig,
                ...config,
            };
        }
        this._markMutation();
    }

    getDocumentStyle() {
        return this.snapshot.documentStyle;
    }

    updateDocumentStyle(config: IDocumentStyle) {
        if (this.snapshot.documentStyle == null) {
            this.snapshot.documentStyle = config;
        } else {
            this.snapshot.documentStyle = {
                ...this.snapshot.documentStyle,
                ...config,
            };
        }
        this._markMutation();
    }

    updateDocumentDataMargin(data: IPaddingData) {
        const { t, l, b, r } = data;
        const { documentStyle } = this.snapshot;

        if (t != null) {
            documentStyle.marginTop = t;
        }

        if (l != null) {
            documentStyle.marginLeft = l;
        }

        if (b != null) {
            documentStyle.marginBottom = b;
        }

        if (r != null) {
            documentStyle.marginRight = r;
        }
        this._markMutation();
    }

    updateDocumentDataPageSize(width?: number, height?: number) {
        const { documentStyle } = this.snapshot;

        if (!documentStyle.pageSize) {
            documentStyle.pageSize = {
                width: width ?? Number.POSITIVE_INFINITY,
                height: height ?? Number.POSITIVE_INFINITY,
            };
            this._markMutation();
            return;
        }

        if (width !== undefined) {
            documentStyle.pageSize.width = width;
        }

        if (height !== undefined) {
            documentStyle.pageSize.height = height;
        }
        this._markMutation();
    }

    updateDrawing(id: string, config: IDrawingUpdateConfig) {
        const { drawings } = this;
        const { width, height, left, top } = config;
        const drawing = drawings?.[id];

        if (!drawing) {
            return;
        }

        const objectTransform = drawing.docTransform;

        objectTransform.size.width = width;
        objectTransform.size.height = height;

        objectTransform.positionH.posOffset = left;
        objectTransform.positionV.posOffset = top;
        this._markMutation();
    }

    setZoomRatio(zoomRatio: number = 1) {
        if (!this.snapshot.settings) {
            this.snapshot.settings = {
                zoomRatio,
            };
        } else {
            this.snapshot.settings.zoomRatio = zoomRatio;
        }
        this._markMutation();
    }

    setDisabled(disabled: boolean) {
        this.snapshot.disabled = disabled;
        this._markMutation();
    }

    getDisabled() {
        return this.snapshot.disabled;
    }

    getTitle() {
        return this.snapshot.title;
    }

    getMutationRevision(): number {
        return this._mutationRevision;
    }

    protected _markMutation(): void {
        this._mutationRevision++;
    }
}

export class DocumentDataModel extends DocumentDataModelSimple {
    private _unitId: string;

    headerModelMap: Map<string, DocumentDataModel> = new Map();

    footerModelMap: Map<string, DocumentDataModel> = new Map();
    noteModelMap: Map<string, DocumentDataModel> = new Map();
    change$ = new BehaviorSubject<number>(0);

    constructor(snapshot: Partial<IDocumentData>) {
        super(Tools.isEmptyObject(snapshot) ? getEmptySnapshot() : snapshot);

        const UNIT_ID_LENGTH = 6;

        this._unitId = this.snapshot.id ?? generateRandomId(UNIT_ID_LENGTH);

        this._initializeHeaderFooterModel();
        this._name$.next(this.snapshot.title ?? '');
    }

    override dispose() {
        super.dispose();
        this.headerModelMap.forEach((header) => {
            header.dispose();
        });

        this.footerModelMap.forEach((footer) => {
            footer.dispose();
        });
        this.noteModelMap.forEach((note) => note.dispose());
        this.noteModelMap.clear();

        this._name$.complete();
    }

    getDrawings() {
        return this.snapshot.drawings;
    }

    getDrawingsOrder() {
        return this.snapshot.drawingsOrder;
    }

    getCustomRanges() {
        return this.snapshot.body?.customRanges;
    }

    getCustomDecorations() {
        return this.snapshot.body?.customDecorations;
    }

    getSettings() {
        return this.snapshot.settings;
    }

    // TODO: @JOCS do not use reset, please use apply to modify the snapshot.
    reset(snapshot: Partial<IDocumentData>) {
        if (snapshot.id && snapshot.id !== this._unitId) {
            throw new Error('Cannot reset a document model with a different unit id!');
        }

        this.snapshot = createDocumentSnapshot(snapshot);
        this._initializeHeaderFooterModel();
        this._markMutation();
        this.change$.next(this.change$.value + 1);
    }

    getSelfOrHeaderFooterModel(segmentId?: string): DocumentDataModel | null {
        if (segmentId === null || segmentId === undefined || segmentId === '') {
            return this;
        }

        if (this.headerModelMap.has(segmentId)) {
            return this.headerModelMap.get(segmentId)!;
        }

        if (this.footerModelMap.has(segmentId)) {
            return this.footerModelMap.get(segmentId)!;
        }

        if (this.noteModelMap.has(segmentId)) {
            return this.noteModelMap.get(segmentId)!;
        }

        return null;
    }

    override getUnitId() {
        return this._unitId;
    }

    apply(actions: JSONXActions) {
        if (JSONX.isNoop(actions)) {
            return;
        }

        const previousNotes = this.snapshot.notes;
        const changedNotes = new Set<string>();
        let changedInheritedNoteStyles = false;
        const cursor = JSON1.type.readCursor(actions);
        cursor.traverse(null, () => {
            const path = cursor.getPath();
            if (path[0] === 'notes' && typeof path[1] === 'string') {
                changedNotes.add(path[1]);
            }
            if (path[0] === 'styles' || path[0] === 'documentStyle') {
                changedInheritedNoteStyles = true;
            }
        });
        this.snapshot = JSONX.apply(this.snapshot, actions) as unknown as IDocumentData;
        this._markMutation();
        if (changedInheritedNoteStyles || previousNotes !== this.snapshot.notes || changedNotes.size > 0) {
            this._initializeNoteModels(changedInheritedNoteStyles ? undefined : previousNotes, changedNotes);
        }

        // FIXME: @JOCS, ANY better solution to find action that create or delete header/footer?
        if (actions?.some((a) => Array.isArray(a) && (a?.[0] === 'headers' || a?.[0] === 'footers'))) {
            this.headerModelMap.clear();
            this.footerModelMap.clear();
            this._initializeHeaderFooterModel();
        }

        this.change$.next(this.change$.value + 1);
        return this.snapshot;
    }

    sliceBody(startOffset: number, endOffset: number, type = SliceBodyType.copy): Nullable<IDocumentBody> {
        const body = this.getBody();

        if (body == null) {
            return;
        }

        return getBodySlice(body, startOffset, endOffset, false, type);
    }

    private _initializeHeaderFooterModel() {
        this._initializeNoteModels();
        const { headers, footers } = this.getSnapshot();

        if (headers) {
            for (const headerId in headers) {
                const header = headers[headerId];
                this.headerModelMap.set(headerId, new DocumentDataModel(header));
                this.headerModelMap.get(headerId)!.updateDocumentId(this.getUnitId());
            }
        }

        if (footers) {
            for (const footerId in footers) {
                const footer = footers[footerId];
                this.footerModelMap.set(footerId, new DocumentDataModel(footer));
                this.footerModelMap.get(footerId)!.updateDocumentId(this.getUnitId());
            }
        }
    }

    private _initializeNoteModels(previousNotes?: IDocumentData['notes'], changedNotes?: ReadonlySet<string>): void {
        for (const [id, model] of this.noteModelMap) {
            if (changedNotes?.has(id) || previousNotes?.[id] !== this.snapshot.notes?.[id] || !previousNotes?.[id]) {
                model.dispose();
                this.noteModelMap.delete(id);
            }
        }
        for (const [id, note] of Object.entries(this.snapshot.notes ?? {})) {
            if (this.noteModelMap.has(id)) {
                continue;
            }
            const model = new DocumentDataModel({
                ...note,
                id: this.getUnitId(),
                documentStyle: this.snapshot.documentStyle,
                styles: this.snapshot.styles,
            });
            this.noteModelMap.set(id, model);
        }
    }

    override updateDocumentStyle(config: IDocumentStyle) {
        super.updateDocumentStyle(config);
        this._initializeNoteModels();
    }

    override updateDocumentId(unitId: string) {
        super.updateDocumentId(unitId);

        this._unitId = unitId;
    }

    getPlainText() {
        return getPlainText(this.getBody()?.dataStream ?? '');
    }

    getStatistics(options: IDocumentStatisticsOptions = {}): Promise<IDocumentStatistics> {
        return calculateDocumentStatistics(this.getBody()?.dataStream ?? '', options);
    }
}