@univerjs/docs-toc 1.5.1 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,7 +13,8 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
- import type { ICommand, ICustomRange, IDocumentBody } from '@univerjs/core';
16
+ import type { ICommand, ICustomBlock, ICustomRange, IDocStyles, IDocumentBody, IDocumentStyle, ILists, IParagraph, ISpecialCharacterCustomRange, ITables, ITextRun } from '@univerjs/core';
17
+ import { LocaleType } from '@univerjs/core';
17
18
  export interface IUpdateTableOfContentsCommandParams {
18
19
  unitId?: string;
19
20
  rangeId?: string;
@@ -34,13 +35,42 @@ export interface IDeleteTableOfContentsCommandParams {
34
35
  unitId?: string;
35
36
  rangeId?: string;
36
37
  }
38
+ export interface ITableOfContentsHeading {
39
+ bookmarkId?: string;
40
+ headingId: string;
41
+ level: number;
42
+ paragraph: IParagraph;
43
+ startOffset: number;
44
+ /** Printed label supplied by an embedded story whose anchor is in the main body. */
45
+ pageNumber?: number | string;
46
+ text: string;
47
+ /** Generated label before a tab separator; used to retain the number's tab spacing. */
48
+ numberingText?: string;
49
+ textRuns?: ITextRun[];
50
+ customBlocks?: ICustomBlock[];
51
+ specialCharacters?: ISpecialCharacterCustomRange[];
52
+ showPageNumber?: boolean;
53
+ }
54
+ interface ITableOfContentsBuildOptions {
55
+ tabStopOffset?: number;
56
+ availableWidth?: number;
57
+ entries?: ITableOfContentsHeading[];
58
+ showPageNumbers?: boolean;
59
+ styles?: IDocStyles;
60
+ documentStyle?: IDocumentStyle;
61
+ locale?: LocaleType;
62
+ lists?: ILists;
63
+ tableSource?: ITables;
64
+ preserveLeadingParagraph?: boolean;
65
+ resetEntryParagraphFormatting?: boolean;
66
+ entryStyleName?: string;
67
+ }
37
68
  export declare const UpdateTableOfContentsCommand: ICommand<IUpdateTableOfContentsCommandParams>;
38
69
  export declare const DeleteTableOfContentsCommand: ICommand<IDeleteTableOfContentsCommandParams>;
39
70
  export declare const InsertTableOfContentsCommand: ICommand<IInsertTableOfContentsCommandParams>;
40
71
  export declare function buildTableOfContentsInstruction(options?: Pick<IInsertTableOfContentsCommandParams, 'levels' | 'showPageNumbers' | 'rightAlignPageNumbers'>): string;
41
72
  export declare function findTableOfContentsAtOffset(body: IDocumentBody | null | undefined, offset: number | undefined): ICustomRange | undefined;
42
- export declare function buildPageNumberOnlyTableOfContentsBody(body: IDocumentBody, toc: ICustomRange, resolvePageNumber: (offset: number) => number | undefined): IDocumentBody | null;
43
- export declare function buildTableOfContentsBody(body: IDocumentBody, toc: ICustomRange, resolvePageNumber: (offset: number) => number | undefined, idFactory?: () => string, options?: {
44
- tabStopOffset?: number;
45
- }): IDocumentBody | null;
46
- export declare function countTableOfContentsHeadings(body: IDocumentBody | null | undefined, levels?: number): number;
73
+ export declare function buildPageNumberOnlyTableOfContentsBody(body: IDocumentBody, toc: ICustomRange, resolvePageNumber: (offset: number) => number | string | undefined, resolvePageField?: (field: ICustomRange) => number | string | undefined): IDocumentBody | null;
74
+ export declare function buildTableOfContentsBody(body: IDocumentBody, toc: ICustomRange, resolvePageNumber: (offset: number) => number | string | undefined, idFactory?: () => string, options?: ITableOfContentsBuildOptions): IDocumentBody | null;
75
+ export declare function countTableOfContentsHeadings(body: IDocumentBody | null | undefined, levels?: number, styles?: IDocStyles): number;
76
+ export {};
@@ -14,5 +14,5 @@
14
14
  * limitations under the License.
15
15
  */
16
16
  export { buildPageNumberOnlyTableOfContentsBody, buildTableOfContentsBody, buildTableOfContentsInstruction, countTableOfContentsHeadings, DeleteTableOfContentsCommand, findTableOfContentsAtOffset, InsertTableOfContentsCommand, UpdateTableOfContentsCommand, } from './commands/table-of-contents.command';
17
- export type { IDeleteTableOfContentsCommandParams, IInsertTableOfContentsCommandParams, IUpdateTableOfContentsCommandParams, TableOfContentsFormat, TableOfContentsTabLeader, } from './commands/table-of-contents.command';
17
+ export type { IDeleteTableOfContentsCommandParams, IInsertTableOfContentsCommandParams, ITableOfContentsHeading, IUpdateTableOfContentsCommandParams, TableOfContentsFormat, TableOfContentsTabLeader, } from './commands/table-of-contents.command';
18
18
  export { UniverDocsTocPlugin } from './plugin';
package/lib/umd/index.js CHANGED
@@ -1 +1 @@
1
- (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/docs"),require("@univerjs/engine-render"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/docs`,`@univerjs/engine-render`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverDocsToc={},e.UniverCore,e.UniverDocs,e.UniverEngineRender,e.rxjs))})(this,function(e,t,n,r,i){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});async function a(e,r,i,a,o,s,c=(0,t.generateRandomId)()){let l=new t.TextX;l.push(...t.BuildTextUtils.selection.delete([a],r.getBody(),0,o));let u=a.startOffset+o.dataStream.length;e.get(n.DocStateChangeManagerService).flushPendingChanges(i);let d=e.get(t.IUndoRedoService).beginUndoRedoGroup(i,c,`append`);try{return!!e.get(t.ICommandService).syncExecuteCommand(n.RichTextEditingMutation.id,{unitId:i,historyAction:s,actions:t.JSONX.getInstance().editOp(l.serialize(),(0,t.getRichTextEditPath)(r)),textRanges:[{startOffset:u,endOffset:u,collapsed:!0}],debounce:!1,trigger:s})}finally{d.dispose()}}let o={id:`doc.command.update-table-of-contents`,type:t.CommandType.COMMAND,handler:(e,t)=>s(e,t)};async function s(e,i,s=(0,t.generateRandomId)()){var c,l,u;let g=e.get(t.IUniverInstanceService),_=i!=null&&i.unitId?g.getUnit(i.unitId,t.UniverInstanceType.UNIVER_DOC):g.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_DOC),v=_==null?void 0:_.getBody(),b=e.get(n.DocSelectionManagerService).getActiveTextRange(),x=i!=null&&i.rangeId?v==null||(c=v.customRanges)==null?void 0:c.find(e=>e.rangeId===i.rangeId&&d(e)):f(v,b==null?void 0:b.startOffset),S=_==null?void 0:_.getUnitId();if(!_||!v||!x||((l=x.properties)==null?void 0:l.locked)===t.BooleanNumber.TRUE||!S)return!1;let C=e.get(r.IRenderManagerService).getRenderUnitById(S),w=C==null||(u=C.getInjector)==null?void 0:u.call(C);if(!C||!(w!=null&&w.has(n.DocSkeletonManagerService)))return!1;let T=C.with(n.DocSkeletonManagerService).getSkeleton();if(!await p(T))return!1;let E=!1;for(let n=0;n<3;n+=1){var D,O,k;let r=_.getBody(),c=r==null||(D=r.customRanges)==null?void 0:D.find(e=>e.rangeId===x.rangeId&&d(e));if(!r||!c||((O=c.properties)==null?void 0:O.locked)===t.BooleanNumber.TRUE)return!1;let l=e=>{var t;let n=T.findBodyPageIndexByCharIndex(e);return(t=T.getSkeletonData())==null||(t=t.pages[n])==null?void 0:t.pageNumber},u=(i==null?void 0:i.mode)===`pageNumbersOnly`?h(r,c,l):y(r,c,l);if(!u)return!1;if((i==null?void 0:i.mode)===`pageNumbersOnly`&&t.Tools.diffValue((0,t.getBodySlice)(r,c.startIndex,c.endIndex+1,!0,t.SliceBodyType.cut),u)||E&&r.dataStream.slice(c.startIndex,c.endIndex+1)===u.dataStream)return!0;let f=((k=T.getLayoutProgress())==null?void 0:k.generation)??-1;if(E=await a(e,_,S,{startOffset:c.startIndex,endOffset:c.endIndex+1,collapsed:!1},u,o.id,s),!E||n===2)return E;if(!await m(T,f))return!1}return E}let c={id:`doc.command.delete-table-of-contents`,type:t.CommandType.COMMAND,async handler(e,r){var i;let o=e.get(t.IUniverInstanceService),s=r!=null&&r.unitId?o.getUnit(r.unitId,t.UniverInstanceType.UNIVER_DOC):o.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_DOC),l=s==null?void 0:s.getBody(),u=e.get(n.DocSelectionManagerService).getActiveTextRange(),p=r!=null&&r.rangeId?l==null||(i=l.customRanges)==null?void 0:i.find(e=>e.rangeId===r.rangeId&&d(e)):f(l,u==null?void 0:u.startOffset),m=s==null?void 0:s.getUnitId();return!p||!m?!1:a(e,s,m,{startOffset:p.startIndex,endOffset:p.endIndex+1,collapsed:!1},{dataStream:``},c.id)}},l={id:`doc.command.insert-table-of-contents`,type:t.CommandType.COMMAND,async handler(e,i){var o;let c=e.get(t.IUniverInstanceService),d=i!=null&&i.unitId?c.getUnit(i.unitId,t.UniverInstanceType.UNIVER_DOC):c.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_DOC),m=e.get(n.DocSelectionManagerService).getActiveTextRange(),h=d==null?void 0:d.getBody(),g=d==null?void 0:d.getUnitId(),_=g?e.get(r.IRenderManagerService).getRenderUnitById(g):void 0,v=_==null||(o=_.getInjector)==null?void 0:o.call(_);if(!d||!h||!g||!m||m.segmentId||m.startOffset!==m.endOffset||f(h,m.startOffset)||!_||!(v!=null&&v.has(n.DocSkeletonManagerService)))return!1;let b=_.with(n.DocSkeletonManagerService).getSkeleton();if(!await p(b))return!1;let x=Math.min(9,Math.max(1,Math.trunc((i==null?void 0:i.levels)??3))),S=(i==null?void 0:i.showPageNumbers)??!0,C=S&&((i==null?void 0:i.rightAlignPageNumbers)??!0),w=(i==null?void 0:i.tabLeader)??`dots`,T=(i==null?void 0:i.format)??`fromTemplate`,E=(0,t.generateRandomId)(),D={rangeId:E,rangeType:t.CustomRangeType.FIELD,startIndex:m.startOffset,endIndex:m.startOffset,wholeEntity:!1,properties:{fieldType:`TOC`,instruction:u({levels:x,showPageNumbers:S,rightAlignPageNumbers:C}),sourceKind:`complex`,showPageNumbers:S,rightAlignPageNumbers:C,tabLeader:w,format:T,title:i==null?void 0:i.title}},{pageSize:O,marginLeft:k=0,marginRight:A=0}=d.getDocumentStyle(),j=y(h,D,e=>{var t;let n=b.findBodyPageIndexByCharIndex(e);return(t=b.getSkeletonData())==null||(t=t.pages[n])==null?void 0:t.pageNumber},t.generateRandomId,{tabStopOffset:Math.max(72,((O==null?void 0:O.width)??594.3)-k-A)});if(!j)return!1;let M=(0,t.generateRandomId)();return await a(e,d,g,{startOffset:m.startOffset,endOffset:m.startOffset,collapsed:!0},j,l.id,M)?(await s(e,{unitId:g,rangeId:E,mode:`entireTable`},M),!0):!1}};function u(e={}){let t=Math.min(9,Math.max(1,Math.trunc(e.levels??3))),n=e.showPageNumbers??!0,r=n&&(e.rightAlignPageNumbers??!0);return`TOC ${[`\\o "1-${t}"`,`\\h`,`\\z`,`\\u`,...n?[]:[`\\n`],...n&&!r?[`\\p " "`]:[]].join(` `)}`}function d(e){var n;return e.rangeType===t.CustomRangeType.FIELD&&((n=e.properties)==null?void 0:n.fieldType)===`TOC`}function f(e,t){var n;if(t!=null)return e==null||(n=e.customRanges)==null?void 0:n.filter(e=>d(e)&&e.startIndex<=t&&t<=e.endIndex).sort((e,t)=>e.endIndex-e.startIndex-(t.endIndex-t.startIndex))[0]}async function p(e){let t=e.getLayoutProgress();if(t==null||t.complete)return!0;try{return await(0,i.firstValueFrom)(e.layoutProgress$.pipe((0,i.filter)(e=>e.complete),(0,i.take)(1),(0,i.timeout)({first:3e4}))),!0}catch{return!1}}async function m(e,t){try{return await(0,i.firstValueFrom)(e.layoutProgress$.pipe((0,i.filter)(e=>e.generation>t&&e.complete),(0,i.take)(1),(0,i.timeout)({first:3e4}))),!0}catch{return!1}}function h(e,n,r){var i,a,o;if(((i=n.properties)==null?void 0:i.locked)===t.BooleanNumber.TRUE)return null;let s=t.Tools.deepClone((0,t.getBodySlice)(e,n.startIndex,n.endIndex+1,!0,t.SliceBodyType.cut)),c=((a=s.customRanges)==null?void 0:a.filter(e=>{var n,r;return e.rangeType===t.CustomRangeType.FIELD&&((n=e.properties)==null?void 0:n.fieldType)===`PAGEREF`&&((r=e.properties)==null?void 0:r.locked)!==t.BooleanNumber.TRUE}).sort((e,t)=>t.startIndex-e.startIndex))??[];for(let t of c){var l;let n=(l=e.customRanges)==null?void 0:l.find(e=>e.rangeId===t.rangeId),i=n&&g(e,n),a=i==null?void 0:r(i);if(a==null)return null;_(s,t,String(a))}let u=(o=s.customRanges)==null?void 0:o.find(e=>e.rangeId===n.rangeId);return u!=null&&u.properties&&(u.properties.cachedResult=(0,t.getPlainText)(s.dataStream.slice(1,-1)).replaceAll(``,``).replaceAll(``,``)),s}function g(e,n){var r,i,a,o,s;let c=(r=String(((i=n.properties)==null?void 0:i.instruction)??``).match(/PAGEREF\s+"?([^"\s\\]+)"?/i))==null?void 0:r[1];if(c){var l;let n=(l=e.customRanges)==null?void 0:l.find(e=>{var n;return e.rangeType===t.CustomRangeType.BOOKMARK&&((n=e.properties)==null?void 0:n.bookmarkId)===c});if(n)return n.startIndex}let u=(a=e.customRanges)==null?void 0:a.find(e=>e.rangeType===t.CustomRangeType.HYPERLINK&&e.startIndex<=n.startIndex&&n.endIndex<=e.endIndex),d=u==null||(o=u.properties)==null?void 0:o.headingId;return(s=e.paragraphs)==null||(s=s.find(e=>{var t;return((t=e.paragraphStyle)==null?void 0:t.headingId)===d}))==null?void 0:s.startIndex}function _(e,t,n){var r,i;let a=t.startIndex+1,o=t.endIndex;if(t.properties&&(t.properties.cachedResult=n),e.dataStream.slice(a,o)===n)return;let s=n.length-(o-a);e.dataStream=`${e.dataStream.slice(0,a)}${n}${e.dataStream.slice(o)}`,v(e.textRuns,a,o,s),(r=e.paragraphs)==null||r.forEach(e=>{e.startIndex>=o&&(e.startIndex+=s)}),(i=e.customRanges)==null||i.forEach(e=>{e.startIndex>=o?(e.startIndex+=s,e.endIndex+=s):e.endIndex>=o&&(e.endIndex+=s)})}function v(e,t,n,r){e==null||e.forEach(e=>{e.st>=n?(e.st+=r,e.ed+=r):e.ed>t&&(e.ed+=r)})}function y(e,n,r,i=t.generateRandomId,a){var o,s,c,l,u,d,f,p,m;if(((o=n.properties)==null?void 0:o.locked)===t.BooleanNumber.TRUE)return null;let h=String(((s=n.properties)==null?void 0:s.instruction)??``);if(/\\c\s+/i.test(h))return null;let[g,_]=S(h),v=C(e,n,g,_);if(v.length===0)return null;let y=((c=n.properties)==null?void 0:c.showPageNumbers)!==!1&&!/\\n(?:\s|$)/i.test(h),x=y&&((l=n.properties)==null?void 0:l.rightAlignPageNumbers)!==!1&&!/\\p\s+/i.test(h),w=E((u=n.properties)==null?void 0:u.tabLeader),D=O((d=n.properties)==null?void 0:d.format),k=((f=e.customRanges)==null?void 0:f.some(e=>e.rangeId===n.rangeId))??!1?((p=e.paragraphs)==null?void 0:p.filter(e=>e.startIndex>=n.startIndex&&e.startIndex<=n.endIndex))??[]:[],A=typeof((m=n.properties)==null?void 0:m.title)==`string`?n.properties.title.trim():b(e,k),j={dataStream:``,paragraphs:[],customRanges:[]},M=j.customRanges,N=k.find(e=>{var t;return((t=e.styleId)==null?void 0:t.toLowerCase())===`tocheading`}),P=k.filter(e=>e!==N),F=/* @__PURE__ */ new Set,I=``;A&&(j.dataStream+=`${A}\r`,I+=`${A}\r`,j.paragraphs.push((0,t.cloneParagraphWithId)({startIndex:j.dataStream.length-1,paragraphId:(N==null?void 0:N.paragraphId)??``,styleId:`TOCHeading`},F)));for(let[e,n]of v.entries()){let o=y?r(n.startOffset):void 0;if(y&&o==null)return null;let s=j.dataStream.length,c=y?x?` `:` `:``,l=`${n.text}${c}${o??``}`;j.dataStream+=`${n.text}${c}`;let u=n.bookmarkId??n.headingId,d={startIndex:s,endIndex:j.dataStream.length-1,rangeId:i(),rangeType:t.CustomRangeType.HYPERLINK,wholeEntity:!1,properties:{...n.bookmarkId?{bookmarkId:u}:{headingId:u},textStyleMode:`text`}};if(M.push(d),y){let e=j.dataStream.length;j.dataStream+=`${o}`;let n=j.dataStream.length-1;d.endIndex=n,M.push({startIndex:e,endIndex:n,rangeId:i(),rangeType:t.CustomRangeType.FIELD,wholeEntity:!1,properties:{fieldType:`PAGEREF`,instruction:`PAGEREF ${u} \\h`,cachedResult:String(o),sourceKind:`complex`}})}j.dataStream+=`\r`,I+=`${l}\r`,j.paragraphs.push(T(P,e,n.level,j.dataStream.length-1,F,x?a==null?void 0:a.tabStopOffset:void 0,w,D))}return j.dataStream+=``,M.push({startIndex:0,endIndex:j.dataStream.length-1,rangeId:n.rangeId,rangeType:t.CustomRangeType.FIELD,wholeEntity:!1,properties:{...t.Tools.deepClone(n.properties??{}),fieldType:`TOC`,instruction:h,cachedResult:I}}),M.sort((e,t)=>e.startIndex-t.startIndex||t.endIndex-e.endIndex),j}function b(e,n){let r=n.find(e=>{var t;return((t=e.styleId)==null?void 0:t.toLowerCase())===`tocheading`});if(!r)return``;let i=[...e.paragraphs??[]].sort((e,t)=>e.startIndex-t.startIndex),a=i.findIndex(e=>e===r||e.paragraphId===r.paragraphId),o=a>0?i[a-1].startIndex+1:0;for(;e.dataStream[o]===``||e.dataStream[o]===``;)o+=1;return(0,t.getPlainText)(e.dataStream.slice(o,r.startIndex)).trim()}function x(e,t=3){return e?C(e,void 0,1,Math.min(9,Math.max(1,Math.trunc(t)))).length:0}function S(e){let t=e.match(/\\o\s+"(\d+)\s*-\s*(\d+)"/i),n=Math.max(1,Number((t==null?void 0:t[1])??1));return[n,Math.min(9,Math.max(n,Number((t==null?void 0:t[2])??3)))]}function C(e,n,r,i){let a=[...e.paragraphs??[]].sort((e,t)=>e.startIndex-t.startIndex),o=0;return a.flatMap(a=>{var s,c,l;let u=o;for(;e.dataStream[u]===``||e.dataStream[u]===``;)u+=1;o=a.startIndex+1;let d=w(a),f=(s=a.paragraphStyle)==null?void 0:s.headingId;if(!f||d==null||d<r||d>i||n&&u>=n.startIndex&&a.startIndex<=n.endIndex)return[];let p=(0,t.getPlainText)(e.dataStream.slice(u,a.startIndex)).trim();if(!p)return[];let m=(c=e.customRanges)==null?void 0:c.find(e=>{var n;return e.rangeType===t.CustomRangeType.BOOKMARK&&e.startIndex<=a.startIndex&&e.endIndex>=u&&((n=e.properties)==null?void 0:n.bookmarkId)});return[{bookmarkId:m==null||(l=m.properties)==null?void 0:l.bookmarkId,headingId:f,level:d,paragraph:a,startOffset:u,text:p}]})}function w(e){var n,r;let i=(n=e.paragraphStyle)==null?void 0:n.outlineLevel;if(i!=null&&i>=0&&i<=8)return i+1;let a=(r=e.paragraphStyle)==null?void 0:r.namedStyleType;if(a!=null&&a>=t.NamedStyleType.HEADING_1&&a<=t.NamedStyleType.HEADING_5)return a-t.NamedStyleType.HEADING_1+1}function T(e,n,r,i,a,o,s=`dots`,c=`fromTemplate`){let l=e.find(e=>{var t;return((t=e.styleId)==null||(t=t.match(/TOC(\d+)/i))==null?void 0:t[1])===String(r)})??e[Math.min(n,e.length-1)];if(l){var u;return(0,t.cloneParagraphWithId)({...l,startIndex:i,paragraphId:((u=e[n])==null?void 0:u.paragraphId)??``},a)}return{startIndex:i,paragraphId:(0,t.createParagraphId)(a),styleId:`TOC${r}`,paragraphStyle:{indentStart:{v:Math.max(0,r-1)*k(c)},spaceBelow:c===`modern`?{v:4}:c===`classic`?{v:2}:void 0,tabStops:o==null?void 0:[{offset:o,alignment:t.TabStopAlignment.END,leader:D(s)}]}}}function E(e){return e===`none`||e===`dashes`||e===`underline`?e:`dots`}function D(e){return{none:t.TabStopLeader.NONE,dots:t.TabStopLeader.DOT,dashes:t.TabStopLeader.HYPHEN,underline:t.TabStopLeader.UNDERSCORE}[e]}function O(e){return e===`classic`||e===`modern`||e===`simple`?e:`fromTemplate`}function k(e){return{fromTemplate:18,classic:14,modern:20,simple:12}[e]}var A=`@univerjs/docs-toc`,j=`1.5.1`;let M={};function N(e,t){return function(n,r){t(n,r,e)}}function P(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}function F(e){"@babel/helpers - typeof";return F=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},F(e)}function I(e,t){if(F(e)!=`object`||!e)return e;var n;if(typeof Symbol<`u`&&(n=e[Symbol.toPrimitive])!==void 0){var r=n.call(e,t||`default`);if(F(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function L(e){var t=I(e,`string`);return F(t)==`symbol`?t:t+``}function R(e,t,n){return(t=L(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}let z=class extends t.Plugin{constructor(e=M,n,r,i){super(),this._config=e,this._injector=n,this._configService=r,this._commandService=i;let{...a}=(0,t.merge)({},M,this._config);this._configService.setConfig(`docs-toc.config`,a)}onStarting(){[l,o,c].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}};R(z,`pluginName`,`DOCS_TOC_PLUGIN`),R(z,`packageName`,A),R(z,`version`,j),R(z,`type`,t.UniverInstanceType.UNIVER_DOC),z=P([(0,t.DependentOn)(n.UniverDocsPlugin,r.UniverRenderEnginePlugin),N(1,(0,t.Inject)(t.Injector)),N(2,t.IConfigService),N(3,t.ICommandService)],z),e.DeleteTableOfContentsCommand=c,e.InsertTableOfContentsCommand=l,Object.defineProperty(e,"UniverDocsTocPlugin",{enumerable:!0,get:function(){return z}}),e.UpdateTableOfContentsCommand=o,e.buildPageNumberOnlyTableOfContentsBody=h,e.buildTableOfContentsBody=y,e.buildTableOfContentsInstruction=u,e.countTableOfContentsHeadings=x,e.findTableOfContentsAtOffset=f});
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/docs"),require("@univerjs/engine-render"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/docs`,`@univerjs/engine-render`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverDocsToc={},e.UniverCore,e.UniverDocs,e.UniverEngineRender,e.rxjs))})(this,function(e,t,n,r,i){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});async function a(e,r,i,a,o,s,c=(0,t.generateRandomId)()){let l=new t.TextX;l.push(...t.BuildTextUtils.selection.delete([a],r.getBody(),0,o));let u=a.startOffset+o.dataStream.length;e.get(n.DocStateChangeManagerService).flushPendingChanges(i);let d=e.get(t.IUndoRedoService).beginUndoRedoGroup(i,c,`append`);try{return!!e.get(t.ICommandService).syncExecuteCommand(n.RichTextEditingMutation.id,{unitId:i,historyAction:s,actions:t.JSONX.getInstance().editOp(l.serialize(),(0,t.getRichTextEditPath)(r)),textRanges:[{startOffset:u,endOffset:u,collapsed:!0}],debounce:!1,trigger:s})}finally{d.dispose()}}let o={id:`doc.command.update-table-of-contents`,type:t.CommandType.COMMAND,handler:(e,t)=>s(e,t)};async function s(e,i,s=(0,t.generateRandomId)()){var c,l,u;let g=e.get(t.IUniverInstanceService),_=i!=null&&i.unitId?g.getUnit(i.unitId,t.UniverInstanceType.UNIVER_DOC):g.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_DOC),v=_==null?void 0:_.getBody(),y=e.get(n.DocSelectionManagerService).getActiveTextRange(),b=i!=null&&i.rangeId?v==null||(c=v.customRanges)==null?void 0:c.find(e=>e.rangeId===i.rangeId&&d(e)):f(v,y==null?void 0:y.startOffset),C=_==null?void 0:_.getUnitId();if(!_||!v||!b||((l=b.properties)==null?void 0:l.locked)===t.BooleanNumber.TRUE||!C)return!1;let w=e.get(r.IRenderManagerService).getRenderUnitById(C),T=w==null||(u=w.getInjector)==null?void 0:u.call(w);if(!w||!(T!=null&&T.has(n.DocSkeletonManagerService)))return!1;let E=w.with(n.DocSkeletonManagerService).getSkeleton();if(!await p(E))return!1;let D=!1;for(let n=0;n<3;n+=1){var O,k,A;let r=_.getBody(),c=r==null||(O=r.customRanges)==null?void 0:O.find(e=>e.rangeId===b.rangeId&&d(e));if(!r||!c||((k=c.properties)==null?void 0:k.locked)===t.BooleanNumber.TRUE)return!1;let l=e=>{var t;let n=E.findBodyPageIndexByCharIndex(e);return(t=E.getSkeletonData())==null||(t=t.pages[n])==null?void 0:t.pageNumber},u=(i==null?void 0:i.mode)===`pageNumbersOnly`?h(r,c,l):S(r,c,l,void 0,{documentStyle:_.getDocumentStyle(),locale:e.get(t.LocaleService).getCurrentLocale(),styles:_.getSnapshot().styles,lists:_.getSnapshot().lists,tableSource:_.getSnapshot().tableSource,availableWidth:x(r,c.startIndex,_.getDocumentStyle())});if(!u)return!1;if((i==null?void 0:i.mode)===`pageNumbersOnly`&&t.Tools.diffValue((0,t.getBodySlice)(r,c.startIndex,c.endIndex+1,!0,t.SliceBodyType.cut),u)||D&&r.dataStream.slice(c.startIndex,c.endIndex+1)===u.dataStream)return!0;let f=((A=E.getLayoutProgress())==null?void 0:A.generation)??-1;if(D=await a(e,_,C,{startOffset:c.startIndex,endOffset:c.endIndex+1,collapsed:!1},u,o.id,s),!D||n===2)return D;if(!await m(E,f))return!1}return D}let c={id:`doc.command.delete-table-of-contents`,type:t.CommandType.COMMAND,async handler(e,r){var i;let o=e.get(t.IUniverInstanceService),s=r!=null&&r.unitId?o.getUnit(r.unitId,t.UniverInstanceType.UNIVER_DOC):o.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_DOC),l=s==null?void 0:s.getBody(),u=e.get(n.DocSelectionManagerService).getActiveTextRange(),p=r!=null&&r.rangeId?l==null||(i=l.customRanges)==null?void 0:i.find(e=>e.rangeId===r.rangeId&&d(e)):f(l,u==null?void 0:u.startOffset),m=s==null?void 0:s.getUnitId();return!p||!m?!1:a(e,s,m,{startOffset:p.startIndex,endOffset:p.endIndex+1,collapsed:!1},{dataStream:``},c.id)}},l={id:`doc.command.insert-table-of-contents`,type:t.CommandType.COMMAND,async handler(e,i){var o;let c=e.get(t.IUniverInstanceService),d=i!=null&&i.unitId?c.getUnit(i.unitId,t.UniverInstanceType.UNIVER_DOC):c.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_DOC),m=e.get(n.DocSelectionManagerService).getActiveTextRange(),h=d==null?void 0:d.getBody(),g=d==null?void 0:d.getUnitId(),_=g?e.get(r.IRenderManagerService).getRenderUnitById(g):void 0,v=_==null||(o=_.getInjector)==null?void 0:o.call(_);if(!d||!h||!g||!m||m.segmentId||m.startOffset!==m.endOffset||f(h,m.startOffset)||!_||!(v!=null&&v.has(n.DocSkeletonManagerService)))return!1;let y=_.with(n.DocSkeletonManagerService).getSkeleton();if(!await p(y))return!1;let b=Math.min(9,Math.max(1,Math.trunc((i==null?void 0:i.levels)??3))),C=(i==null?void 0:i.showPageNumbers)??!0,w=C&&((i==null?void 0:i.rightAlignPageNumbers)??!0),T=(i==null?void 0:i.tabLeader)??`dots`,E=(i==null?void 0:i.format)??`fromTemplate`,D=(0,t.generateRandomId)(),O={rangeId:D,rangeType:t.CustomRangeType.FIELD,startIndex:m.startOffset,endIndex:m.startOffset,wholeEntity:!1,properties:{fieldType:`TOC`,instruction:u({levels:b,showPageNumbers:C,rightAlignPageNumbers:w}),sourceKind:`complex`,showPageNumbers:C,rightAlignPageNumbers:w,tabLeader:T,format:E,title:i==null?void 0:i.title}},k=x(h,m.startOffset,d.getDocumentStyle()),A=S(h,O,e=>{var t;let n=y.findBodyPageIndexByCharIndex(e);return(t=y.getSkeletonData())==null||(t=t.pages[n])==null?void 0:t.pageNumber},t.generateRandomId,{tabStopOffset:Math.max(72,k??594.3),availableWidth:k,documentStyle:d.getDocumentStyle(),locale:e.get(t.LocaleService).getCurrentLocale(),styles:d.getSnapshot().styles,lists:d.getSnapshot().lists,tableSource:d.getSnapshot().tableSource});if(!A)return!1;let j=(0,t.generateRandomId)();return await a(e,d,g,{startOffset:m.startOffset,endOffset:m.startOffset,collapsed:!0},A,l.id,j)?(await s(e,{unitId:g,rangeId:D,mode:`entireTable`},j),!0):!1}};function u(e={}){let t=Math.min(9,Math.max(1,Math.trunc(e.levels??3))),n=e.showPageNumbers??!0,r=n&&(e.rightAlignPageNumbers??!0);return`TOC ${[`\\o "1-${t}"`,`\\h`,`\\z`,`\\u`,...n?[]:[`\\n`],...n&&!r?[`\\p " "`]:[]].join(` `)}`}function d(e){var n;return e.rangeType===t.CustomRangeType.FIELD&&((n=e.properties)==null?void 0:n.fieldType)===`TOC`}function f(e,t){var n;if(t!=null)return e==null||(n=e.customRanges)==null?void 0:n.filter(e=>d(e)&&e.startIndex<=t&&t<=e.endIndex).sort((e,t)=>e.endIndex-e.startIndex-(t.endIndex-t.startIndex))[0]}async function p(e){let t=e.getLayoutProgress();if(t==null||t.complete)return!0;try{return await(0,i.firstValueFrom)(e.layoutProgress$.pipe((0,i.filter)(e=>e.complete),(0,i.take)(1),(0,i.timeout)({first:3e4}))),!0}catch{return!1}}async function m(e,t){let n=e.getLayoutProgress();if(n==null||n.complete&&n.generation>t)return!0;try{return await(0,i.firstValueFrom)(e.layoutProgress$.pipe((0,i.filter)(e=>e.generation>t&&e.complete),(0,i.take)(1),(0,i.timeout)({first:3e4}))),!0}catch{return!1}}function h(e,n,r,i){var a,o,s;if(((a=n.properties)==null?void 0:a.locked)===t.BooleanNumber.TRUE)return null;let c=t.Tools.deepClone((0,t.getBodySlice)(e,n.startIndex,n.endIndex+1,!0,t.SliceBodyType.cut)),l=((o=c.customRanges)==null?void 0:o.filter(e=>{var n,r;return e.rangeType===t.CustomRangeType.FIELD&&((n=e.properties)==null?void 0:n.fieldType)===`PAGEREF`&&((r=e.properties)==null?void 0:r.locked)!==t.BooleanNumber.TRUE}).sort((e,t)=>t.startIndex-e.startIndex))??[];for(let t of l){var u;let n=(u=e.customRanges)==null?void 0:u.find(e=>e.rangeId===t.rangeId),a=n&&g(e,n),o=(n&&(i==null?void 0:i(n)))??(a==null?void 0:r(a));if(o==null)return null;y(c,t,String(o))}let d=(s=c.customRanges)==null?void 0:s.find(e=>e.rangeId===n.rangeId);return d!=null&&d.properties&&(d.properties.cachedResult=(0,t.getPlainText)(c.dataStream.slice(1,-1)).replaceAll(``,``).replaceAll(``,``)),c}function g(e,n){var r,i,a,o;let s=(r=String(((i=n.properties)==null?void 0:i.instruction)??``).match(/PAGEREF\s+"?([^"\s\\]+)"?/i))==null?void 0:r[1];if(s){var c;let n=(c=e.customRanges)==null?void 0:c.find(e=>{var n;return e.rangeType===t.CustomRangeType.BOOKMARK&&((n=e.properties)==null?void 0:n.bookmarkId)===s});if(n)return n.startIndex;let r=_(e,s);if(r!=null)return r}let l=(a=e.customRanges)==null?void 0:a.find(e=>e.rangeType===t.CustomRangeType.HYPERLINK&&e.startIndex<=n.startIndex&&n.endIndex<=e.endIndex);return _(e,l==null||(o=l.properties)==null?void 0:o.headingId)}function _(e,t){var n;if(!t||!e.paragraphs)return;let r=e.paragraphs.findIndex(e=>{var n;return((n=e.paragraphStyle)==null?void 0:n.headingId)===t||e.paragraphId===t});if(!(r<0))return v(e.dataStream,(((n=e.paragraphs[r-1])==null?void 0:n.startIndex)??-1)+1)}function v(e,t){for(;e[t]===``||e[t]===``;)t+=1;return t}function y(e,t,n){var r,i;let a=t.startIndex+1,o=t.endIndex;if(t.properties&&(t.properties.cachedResult=n),e.dataStream.slice(a,o)===n)return;let s=n.length-(o-a);e.dataStream=`${e.dataStream.slice(0,a)}${n}${e.dataStream.slice(o)}`,b(e.textRuns,a,o,s),(r=e.paragraphs)==null||r.forEach(e=>{e.startIndex>=o&&(e.startIndex+=s)}),(i=e.customRanges)==null||i.forEach(e=>{e.startIndex>=o?(e.startIndex+=s,e.endIndex+=s):e.endIndex>=o&&(e.endIndex+=s)})}function b(e,t,n,r){e==null||e.forEach(e=>{e.st>=n?(e.st+=r,e.ed+=r):e.ed>t&&(e.ed+=r)})}function x(e,t,n){var r,i,a,o;let s=(r=e.sectionBreaks)==null?void 0:r.find(e=>t<=e.startIndex),c=(s==null||(i=s.pageSize)==null?void 0:i.width)??(n==null||(a=n.pageSize)==null?void 0:a.width);return(s==null||(o=s.columnProperties)==null||(o=o[0])==null?void 0:o.width)??(c==null?void 0:c-((s==null?void 0:s.marginLeft)??(n==null?void 0:n.marginLeft)??0)-((s==null?void 0:s.marginRight)??(n==null?void 0:n.marginRight)??0))}function S(e,n,r,i=t.generateRandomId,a){var o,s,c,l,u,d,f,p,m,h;if(((o=n.properties)==null?void 0:o.locked)===t.BooleanNumber.TRUE)return null;a={...a,availableWidth:(a==null?void 0:a.availableWidth)??x(e,n.startIndex,a==null?void 0:a.documentStyle)};let g=String(((s=n.properties)==null?void 0:s.instruction)??``),_=g.replace(/"[^"]*"/g,``).split(/\s+/).some(e=>e.toLowerCase()===`\\h`),v=D(_,a),y=k(a);if(/\\c\s+/i.test(g)&&!(a!=null&&a.entries))return null;let[b,S]=E(g),T=(a==null?void 0:a.entries)??j(e,n,b,S,a==null?void 0:a.styles,a??{});if(T.length===0)return null;let M=(a==null?void 0:a.showPageNumbers)??(((c=n.properties)==null?void 0:c.showPageNumbers)!==!1&&!/\\n(?:\s|$)/i.test(g)),I=M&&((l=n.properties)==null?void 0:l.rightAlignPageNumbers)!==!1&&!/\\p\s+/i.test(g),R=F((u=n.properties)==null?void 0:u.tabLeader),z=L((d=n.properties)==null?void 0:d.format),B=((f=e.customRanges)==null?void 0:f.some(e=>e.rangeId===n.rangeId))??!1,V=B?((p=e.paragraphs)==null?void 0:p.filter(t=>t.startIndex>=n.startIndex&&(t.startIndex<=n.endIndex||t.startIndex===n.endIndex+1&&e.dataStream[t.startIndex]===`\r`&&/^(toc[1-9]|tableoffigures)$/.test(C(t,a==null?void 0:a.styles)))))??[]:[],H=typeof((m=n.properties)==null?void 0:m.title)==`string`?n.properties.title.trim():w(e,V,n.startIndex,a==null?void 0:a.styles),U={dataStream:``,paragraphs:[],customRanges:[]},W=U.customRanges,G=V.find(e=>C(e,a==null?void 0:a.styles)===`tocheading`),K=B&&((h=e.paragraphs)==null?void 0:h.some(e=>e.startIndex===n.startIndex-1&&C(e,a==null?void 0:a.styles)===`tocheading`)),q=B&&e.dataStream[n.startIndex+1]===`\r`,J=q?V.find(e=>e.startIndex===n.startIndex+1):void 0,Y=V.filter(e=>e!==G&&e!==J),X=N(e,n,Y),Z=/* @__PURE__ */ new Set,Q=``;!B&&H&&(U.dataStream=`${H}\r`,U.paragraphs.push((0,t.cloneParagraphWithId)({startIndex:U.dataStream.length-1,paragraphId:``,styleId:`TOCHeading`},Z)));let ee=U.dataStream.length;U.dataStream+=``,((a==null?void 0:a.preserveLeadingParagraph)===!0||q&&(a==null?void 0:a.preserveLeadingParagraph)!==!1)&&(U.dataStream+=`\r`,Q+=`\r`,U.paragraphs.push((0,t.cloneParagraphWithId)({...t.Tools.deepClone(J??{paragraphId:``,startIndex:1}),startIndex:U.dataStream.length-1},Z))),H&&B&&!K&&(U.dataStream+=`${H}\r`,Q+=`${H}\r`,U.paragraphs.push((0,t.cloneParagraphWithId)({...t.Tools.deepClone(G??{}),startIndex:U.dataStream.length-1,paragraphId:(G==null?void 0:G.paragraphId)??``,styleId:(G==null?void 0:G.styleId)??`TOCHeading`},Z)));for(let[e,o]of T.entries()){var $,te,ne,re;let s=O(o,v),c=M&&s.showPageNumber!==!1,l=c?s.pageNumber??r(s.startOffset):void 0;if(c&&l==null)return null;let u=U.dataStream.length;($=s.specialCharacters)!=null&&$.length&&W.push(...s.specialCharacters.map(e=>({...t.Tools.deepClone(e),rangeId:i(),startIndex:u+e.startIndex,endIndex:u+e.endIndex}))),(te=s.customBlocks)!=null&&te.length&&(U.customBlocks??=[],U.customBlocks.push(...s.customBlocks.map(e=>({...e,startIndex:u+e.startIndex})))),(ne=s.textRuns)!=null&&ne.length&&(U.textRuns??=[],U.textRuns.push(...s.textRuns.map(e=>({...t.Tools.deepClone(e),st:u+e.st,ed:u+e.ed}))));let d=c?I?` `:` `:``,f=`${s.text}${d}${l??``}`;U.dataStream+=`${s.text}${d}`;let p=s.bookmarkId??s.headingId,m={startIndex:u,endIndex:U.dataStream.length-1,rangeId:i(),rangeType:t.CustomRangeType.HYPERLINK,wholeEntity:!1,properties:{...s.bookmarkId?{bookmarkId:p}:{headingId:p},textStyleMode:`text`}};if(_&&(s.text.length||c)&&W.push(m),c){let e=U.dataStream.length;U.dataStream+=`${l}`;let n=U.dataStream.length-1;m.endIndex=n,W.push({startIndex:e,endIndex:n,rangeId:i(),rangeType:t.CustomRangeType.FIELD,wholeEntity:!1,properties:{fieldType:`PAGEREF`,instruction:`PAGEREF ${p}${_?` \\h`:``}`,cachedResult:String(l),sourceKind:`complex`}})}U.dataStream+=`\r`,Q+=`${f}\r`;let h=P(Y,e,s.level,U.dataStream.length-1,Z,{tabStopOffset:I?a==null?void 0:a.tabStopOffset:void 0,tabLeader:((re=n.properties)==null?void 0:re.tabLeader)==null?void 0:R,format:z,styles:a==null?void 0:a.styles,refreshPageTab:I,resetEntryParagraphFormatting:a==null?void 0:a.resetEntryParagraphFormatting,styleName:a==null?void 0:a.entryStyleName,sourceParagraph:X.get(s.bookmarkId??s.headingId)??X.get(s.paragraph.paragraphId)});if(!s.text&&I&&c){let e=(0,t.resolveDocumentParagraphStyle)(a==null?void 0:a.documentStyle,h.paragraphStyle,{styles:a==null?void 0:a.styles,paragraphStyleId:h.styleId}).tabStops??[],n=e.find(e=>!e.clear&&e.alignment===t.TabStopAlignment.END);n&&(h.paragraphStyle={...h.paragraphStyle,tabStops:e.map(e=>e.offset<n.offset?{...e,clear:!0}:e)})}A(h,s,n.rangeId,y,a),U.paragraphs.push(h)}return U.dataStream+=``,W.push({startIndex:ee,endIndex:U.dataStream.length-1,rangeId:n.rangeId,rangeType:t.CustomRangeType.FIELD,wholeEntity:!1,properties:{...t.Tools.deepClone(n.properties??{}),fieldType:`TOC`,instruction:g,cachedResult:Q}}),B||(U.dataStream+=`\r`,U.paragraphs.push((0,t.cloneParagraphWithId)({startIndex:U.dataStream.length-1,paragraphId:``,paragraphStyle:{outlineLevel:9}},Z))),W.sort((e,t)=>e.startIndex-t.startIndex||t.endIndex-e.endIndex),U}function C(e,t){var n;let r=(e.styleId??``).replace(/\s+/g,``).toLowerCase();return/^toc(?:[1-9]|heading)$/.test(r)?r:((t==null||(n=t[e.styleId??``])==null?void 0:n.name)??r).replace(/\s+/g,``).toLowerCase()}function w(e,n,r,i){let a=n.find(e=>C(e,i)===`tocheading`);if(!a)return``;let o=[...e.paragraphs??[]].sort((e,t)=>e.startIndex-t.startIndex),s=o.findIndex(e=>e===a||e.paragraphId===a.paragraphId),c=s>0?o[s-1].startIndex+1:0;for(c=Math.max(c,r+1);e.dataStream[c]===``||e.dataStream[c]===``;)c+=1;return(0,t.getPlainText)(e.dataStream.slice(c,a.startIndex)).trim()}function T(e,t=3,n){return e?j(e,void 0,1,Math.min(9,Math.max(1,Math.trunc(t))),n).length:0}function E(e){let t=e.match(/\\o\s+"(\d+)\s*-\s*(\d+)"/i),n=Math.max(1,Number((t==null?void 0:t[1])??1));return[n,Math.min(9,Math.max(n,Number((t==null?void 0:t[2])??3)))]}function D(e,n){var r,i;if(!e||!n.resetEntryParagraphFormatting||((r=n.documentStyle)==null?void 0:r.documentFlavor)===t.DocumentFlavor.MODERN)return;let{styles:a}=n,o=(i=Object.entries(a??{}).find(([e,n])=>n.type===t.DocStyleType.character&&(n.name??e).toLowerCase()===`hyperlink`))==null?void 0:i[0],s=(0,t.resolveDocumentTextStyle)(a,o),c=Object.fromEntries([`ff`,`eastAsiaFontFamily`,`eastAsiaFontTheme`,`fontHint`].filter(e=>s[e]!==void 0).map(e=>[e,s[e]]));return Object.keys(c).length?c:void 0}function O(e,t){if(!t)return e;let n=[],r=0;for(let i of e.textRuns??[])r<i.st&&n.push({st:r,ed:i.st,ts:t}),n.push({...i,ts:{...t,...i.ts}}),r=i.ed;return r<e.text.length&&n.push({st:r,ed:e.text.length,ts:t}),{...e,textRuns:n}}function k(e){if(!(e!=null&&e.resetEntryParagraphFormatting)||!e.styles)return[];let{styles:n,documentStyle:i}=e;return Object.entries(n).filter(([e,n])=>n.type===t.DocStyleType.paragraph&&/^toc[1-9]$/.test((n.name??e).replace(/\s+/g,``).toLowerCase())).map(([e])=>(0,r.getNumberUnitValue)((0,t.resolveDocumentParagraphStyle)(i,void 0,{styles:n,paragraphStyleId:e}).indentStart,0)).filter(e=>e>0).sort((e,t)=>e-t)}function A(e,n,i,a,o){var s,c,l,u,d,f,p;let m=n.text.indexOf(` `),h=n.numberingText??(o!=null&&o.resetEntryParagraphFormatting&&m>0?n.text.slice(0,m):void 0);if(!h||(o==null||(s=o.documentStyle)==null?void 0:s.documentFlavor)===t.DocumentFlavor.MODERN)return;n.numberingText&&o!=null&&o.resetEntryParagraphFormatting&&(c=e.paragraphStyle)!=null&&c.tabStops&&(e.paragraphStyle.tabStops=e.paragraphStyle.tabStops.filter(e=>!e.clear&&e.alignment===t.TabStopAlignment.END));let g=(0,t.resolveDocumentParagraphStyle)(o==null?void 0:o.documentStyle,e.paragraphStyle,{styles:o==null?void 0:o.styles,paragraphStyleId:e.styleId}),_=(l=n.textRuns)==null||(l=l.find(e=>e.st===0&&e.ed>=h.length))==null?void 0:l.ts,v=(0,r.resolveDocumentDefaultFont)({...g.textStyle,..._},o==null?void 0:o.documentStyle,(o==null?void 0:o.locale)??t.LocaleType.EN_US),y=(0,r.getFontStyleString)(v,o==null||(u=o.documentStyle)==null?void 0:u.fontFamilyFallbacks,o==null||(d=o.documentStyle)==null?void 0:d.fontFamilyClasses),b=(0,r.measureDocumentNoWrapTextWidth)({id:i,documentStyle:{...o==null?void 0:o.documentStyle,textStyle:v},body:{dataStream:h,textRuns:(f=n.textRuns)==null?void 0:f.filter(e=>e.st<h.length)}},a.length>0),x=y.originFontSize*4/3,S=(0,r.getNumberUnitValue)(g.indentFirstLine,x),C=(0,r.getNumberUnitValue)(g.indentStart,x),w=S>0?0:(0,r.getNumberUnitValue)(g.hanging,x),T=C+Math.max(S,0)-w+b,E=g.tabStops??[],D=E.filter(e=>!e.clear&&e.offset>T).sort((e,t)=>e.offset-t.offset)[0];if(D&&(D.alignment===t.TabStopAlignment.START||D.alignment===t.TabStopAlignment.TAB_STOP_ALIGNMENT_UNSPECIFIED)&&((o==null?void 0:o.availableWidth)==null||D.offset<o.availableWidth)){var O;(O=e.paragraphStyle)!=null&&O.tabStops&&(e.paragraphStyle={...e.paragraphStyle,tabStops:[...e.paragraphStyle.tabStops].sort((e,t)=>e.offset-t.offset)});return}let k=w>0&&(o==null||(p=o.documentStyle)==null||(p=p.compatibilityFlags)==null?void 0:p.noTabHangInd)!==t.BooleanNumber.TRUE&&C>T&&C<Math.min((D==null?void 0:D.offset)??1/0,(o==null?void 0:o.availableWidth)??1/0),A=44/3,j=a.find(e=>e>=T+A-1e-6&&e<Math.min((D==null?void 0:D.offset)??1/0,(o==null?void 0:o.availableWidth)??1/0)),M=k?C:j??(Math.floor(T/A+1e-6)+2)*11*4/3;e.paragraphStyle={...e.paragraphStyle,tabStops:[...E,{offset:M,alignment:t.TabStopAlignment.START,leader:t.TabStopLeader.NONE}].sort((e,t)=>e.offset-t.offset)}}function j(e,n,i,a,o,s){let c=s&&{body:e,styles:o,documentStyle:s.documentStyle,lists:{...t.PRESET_LIST_TYPE,...s.lists},tableSource:s.tableSource},l=c&&(0,r.resolveDocumentParagraphNumbering)(c),u=[...e.paragraphs??[]].sort((e,t)=>e.startIndex-t.startIndex),d=0;return u.flatMap(r=>{var u,f,p,m,h,g,_,y;let b=v(e.dataStream,d);d=r.startIndex+1;let x=M(r,o),S=((u=r.paragraphStyle)==null?void 0:u.headingId)||r.paragraphId;if(!S||x==null||x<i||x>a||n&&b>=n.startIndex&&r.startIndex<=n.endIndex)return[];let C=(e.customRanges??[]).filter(n=>{var i,a;return n.rangeType===t.CustomRangeType.CUSTOM&&n.wholeEntity===!0&&n.startIndex===n.endIndex&&n.startIndex>=b&&n.endIndex<r.startIndex&&(((i=n.properties)==null?void 0:i.kind)===t.SpecialCharacterKind.NON_BREAKING_HYPHEN&&e.dataStream[n.startIndex]===`‑`||((a=n.properties)==null?void 0:a.kind)===t.SpecialCharacterKind.OPTIONAL_HYPHEN&&e.dataStream[n.startIndex]===`­`)}),w=new Set(C.filter(e=>e.properties.kind===t.SpecialCharacterKind.OPTIONAL_HYPHEN).map(e=>e.startIndex)),T=[],E=``;for(let n=b;n<r.startIndex;n++)T[n-b]=E.length,w.has(n)||(E+=(0,t.getPlainText)(e.dataStream[n]));let D=E.length-E.trimStart().length,O=E.trim();if(!O)return[];let k=(f=e.customRanges)==null?void 0:f.find(e=>{var n;return e.rangeType===t.CustomRangeType.BOOKMARK&&e.startIndex<=r.startIndex&&e.endIndex>=b&&((n=e.properties)==null?void 0:n.bookmarkId)}),A=l==null||(p=l.get(r.startIndex))==null?void 0:p.label,j=c&&(0,t.resolveDocumentParagraphBullet)(c,r),N=j&&(c==null?void 0:c.lists[j.listType]),P=j&&(N==null||(m=N.nestingLevel[j.nestingLevel])==null?void 0:m.followNumberWith),F=j&&(s==null||(h=s.lists)==null?void 0:h[j.listType])==null&&t.PRESET_LIST_TYPE[j.listType],I=P===0||P==null&&F?` `:` `,L=A?A.replace(/[ \t]+$/,``)+I:``;A&&!/\\w(?:\s|$)/i.test(String((n==null||(g=n.properties)==null?void 0:g.instruction)??``))&&(O=O.replace(/\t/g,` `));let R=(0,t.resolveDocumentParagraphStyle)(s==null?void 0:s.documentStyle,r.paragraphStyle,{styles:o,paragraphStyleId:r.styleId}),z=!F&&j?N==null||(_=N.nestingLevel[j.nestingLevel])==null?void 0:_.textStyle:void 0,B={...R.paragraphMarkTextStyle,...j==null?void 0:j.textStyle,...z},V=R.textStyle,H=Object.fromEntries([`ff`,`eastAsiaFontFamily`,`eastAsiaFontTheme`,`complexScriptFontFamily`,`fontHint`,`bl`,`it`].filter(e=>B[e]!==void 0&&(e===`bl`||e===`it`||B[e]!==(V==null?void 0:V[e]))).map(e=>[e,B[e]]));return[{bookmarkId:k==null||(y=k.properties)==null?void 0:y.bookmarkId,headingId:S,level:x,paragraph:r,startOffset:b,text:`${L}${O}`,numberingText:L&&I===` `?L.slice(0,-1):void 0,textRuns:L&&Object.keys(H).length?[{st:0,ed:L.length-I.length,ts:H}]:void 0,specialCharacters:C.filter(e=>e.properties.kind===t.SpecialCharacterKind.NON_BREAKING_HYPHEN).map(e=>({...e,startIndex:L.length+T[e.startIndex-b]-D,endIndex:L.length+T[e.endIndex-b]-D}))}]})}function M(e,n){let r=(0,t.resolveDocumentParagraphStyle)(void 0,e.paragraphStyle,{styles:n,paragraphStyleId:e.styleId}),i=r.outlineLevel;if(i!=null)return i>=0&&i<=8?i+1:void 0;let a=r.namedStyleType;if(a!=null&&a>=t.NamedStyleType.HEADING_1&&a<=t.NamedStyleType.HEADING_5)return a-t.NamedStyleType.HEADING_1+1}function N(e,n,r){let i=/* @__PURE__ */ new Map;if(!r.length)return i;let a=e.paragraphs??[],o=a.map(e=>e.startIndex),s=new Map(r.map(e=>[e.startIndex,e])),c=new Map((e.customRanges??[]).filter(e=>{var n;return e.rangeType===t.CustomRangeType.BOOKMARK&&((n=e.properties)==null?void 0:n.bookmarkId)}).map(e=>[String(e.properties.bookmarkId).toLowerCase(),e.startIndex]));for(let r of e.customRanges??[]){var l,u,d;if(r.rangeType!==t.CustomRangeType.HYPERLINK||r.startIndex<=n.startIndex||r.endIndex>=n.endIndex)continue;let e=s.get((l=a[(0,t.binSearchFirstGreaterThanTarget)(o,r.startIndex-1)])==null?void 0:l.startIndex),p=((u=r.properties)==null?void 0:u.bookmarkId)??((d=r.properties)==null?void 0:d.headingId);if(!e||!p)continue;i.set(p,e);let m=c.get(p.toLowerCase());if(m!=null){let n=a[(0,t.binSearchFirstGreaterThanTarget)(o,m-1)];if(n){var f;i.set(n.paragraphId,e),(f=n.paragraphStyle)!=null&&f.headingId&&i.set(n.paragraphStyle.headingId,e)}}}return i}function P(e,n,r,i,a,o={}){var s,c;let{tabStopOffset:l,tabLeader:u,format:d=`fromTemplate`,styles:f,refreshPageTab:p=!1,resetEntryParagraphFormatting:m=!1}=o,h=o.styleName??`toc${r}`,g=e[n],_=g&&C(g,f)===h?g:e.find(e=>C(e,f)===h),v=(o.sourceParagraph&&C(o.sourceParagraph,f)===h?o.sourceParagraph:void 0)??_??e[Math.min(n,e.length-1)],y=h===`tableoffigures`?`TableOfFigures`:`TOC${r}`,b=f==null?void 0:f[y],x=(b==null?void 0:b.type)===t.DocStyleType.paragraph&&(b.name??y).replace(/\s+/g,``).toLowerCase()===h?y:Object.keys(f??{}).find(e=>f[e].type===t.DocStyleType.paragraph&&(f[e].name??e).replace(/\s+/g,``).toLowerCase()===h),S=!(v!=null&&v.styleId)||C(v,f)===`normal`||m&&C(v,f)!==h,w=v==null?void 0:v.styleId;S&&(w=x??w,m&&x==null&&(w=y));let T=(0,t.resolveDocumentParagraphStyle)(void 0,void 0,{styles:f,paragraphStyleId:w}).tabStops??[],E=(v==null||(s=v.paragraphStyle)==null?void 0:s.tabStops)??[],D=T.filter(e=>e.alignment===t.TabStopAlignment.END),O=l??((c=E.filter(e=>e.alignment===t.TabStopAlignment.END).pop())==null?void 0:c.offset),k=D.map(e=>({...e,...u==null?{}:{leader:I(u)}}));!k.length&&O!=null&&k.push({offset:O,alignment:t.TabStopAlignment.END,leader:I(u??`dots`)});let A=p&&k.length?[...E.filter(e=>e.alignment!==t.TabStopAlignment.END),...k]:void 0,j=m&&d===`fromTemplate`&&x==null&&/^toc[1-9]$/.test(h)?{indentStart:{v:Math.max(0,r-1)*2,u:t.NumberUnitType.CHARACTER}}:{};if(v){var M;let r=v.paragraphStyle;if(m){let e=A??E.filter(e=>e.alignment!==t.TabStopAlignment.END);r={...j,...e.length?{tabStops:e}:{}}}else A&&(r={...r,tabStops:A});return(0,t.cloneParagraphWithId)({...v,...m||A?{paragraphStyle:r}:{},...S?{styleId:w}:{},startIndex:i,paragraphId:((M=e[n])==null?void 0:M.paragraphId)??``},a)}let N={indentStart:{v:Math.max(0,r-1)*R(d)}};return(d===`modern`||d===`classic`)&&(N.spaceBelow={v:d===`modern`?4:2}),A?N.tabStops=A:l!=null&&(N.tabStops=[{offset:l,alignment:t.TabStopAlignment.END,leader:I(u??`dots`)}]),m&&(N={...j,...A!=null&&A.length?{tabStops:A}:{}}),{startIndex:i,paragraphId:(0,t.createParagraphId)(a),styleId:x??y,paragraphStyle:N}}function F(e){return e===`none`||e===`dashes`||e===`underline`?e:`dots`}function I(e){return{none:t.TabStopLeader.NONE,dots:t.TabStopLeader.DOT,dashes:t.TabStopLeader.HYPHEN,underline:t.TabStopLeader.UNDERSCORE}[e]}function L(e){return e===`classic`||e===`modern`||e===`simple`?e:`fromTemplate`}function R(e){return{fromTemplate:18,classic:14,modern:20,simple:12}[e]}var z=`@univerjs/docs-toc`,B=`1.6.0`;let V={};function H(e,t){return function(n,r){t(n,r,e)}}function U(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}function W(e){"@babel/helpers - typeof";return W=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},W(e)}function G(e,t){if(W(e)!=`object`||!e)return e;var n;if(typeof Symbol<`u`&&(n=e[Symbol.toPrimitive])!==void 0){var r=n.call(e,t||`default`);if(W(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function K(e){var t=G(e,`string`);return W(t)==`symbol`?t:t+``}function q(e,t,n){return(t=K(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}let J=class extends t.Plugin{constructor(e=V,n,r,i){super(),this._config=e,this._injector=n,this._configService=r,this._commandService=i;let{...a}=(0,t.merge)({},V,this._config);this._configService.setConfig(`docs-toc.config`,a)}onStarting(){[l,o,c].forEach(e=>this.disposeWithMe(this._commandService.registerCommand(e)))}};q(J,`pluginName`,`DOCS_TOC_PLUGIN`),q(J,`packageName`,z),q(J,`version`,B),q(J,`type`,t.UniverInstanceType.UNIVER_DOC),J=U([(0,t.DependentOn)(n.UniverDocsPlugin,r.UniverRenderEnginePlugin),H(1,(0,t.Inject)(t.Injector)),H(2,t.IConfigService),H(3,t.ICommandService)],J),e.DeleteTableOfContentsCommand=c,e.InsertTableOfContentsCommand=l,Object.defineProperty(e,"UniverDocsTocPlugin",{enumerable:!0,get:function(){return J}}),e.UpdateTableOfContentsCommand=o,e.buildPageNumberOnlyTableOfContentsBody=h,e.buildTableOfContentsBody=S,e.buildTableOfContentsInstruction=u,e.countTableOfContentsHeadings=T,e.findTableOfContentsAtOffset=f});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@univerjs/docs-toc",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "private": false,
5
5
  "description": "Table of contents commands for Univer Docs.",
6
6
  "author": "DreamNum Co., Ltd. <developer@univer.ai>",
@@ -52,15 +52,15 @@
52
52
  "rxjs": ">=7.0.0"
53
53
  },
54
54
  "dependencies": {
55
- "@univerjs/core": "1.5.1",
56
- "@univerjs/docs": "1.5.1",
57
- "@univerjs/engine-render": "1.5.1"
55
+ "@univerjs/core": "1.6.0",
56
+ "@univerjs/docs": "1.6.0",
57
+ "@univerjs/engine-render": "1.6.0"
58
58
  },
59
59
  "devDependencies": {
60
- "@univerjs-infra/shared": "1.5.1",
60
+ "@univerjs-infra/shared": "1.6.0",
61
61
  "rxjs": "7.8.2",
62
- "tsrs": "0.23.0-ts7.1.0-beta",
63
- "vitest": "^5.1.0"
62
+ "tsrs": "7.1.0-beta.tsrs2",
63
+ "vitest": "^5.1.1"
64
64
  },
65
65
  "scripts": {
66
66
  "test": "vitest run",