@univerjs/docs-find-replace 1.0.0-insiders.20260727-46ed76c

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.
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Copyright 2023-present DreamNum Co., Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import type { DocumentDataModel, ITextRange, Nullable } from '@univerjs/core';
17
+ import type { DocSkeletonManagerService } from '@univerjs/docs';
18
+ import type { IFindMatch, IFindMoveParams, IFindQuery, IReplaceAllResult } from '@univerjs/find-replace';
19
+ import { ICommandService, ThemeService } from '@univerjs/core';
20
+ import { DocSelectionManagerService } from '@univerjs/docs';
21
+ import { IRenderManagerService } from '@univerjs/engine-render';
22
+ import { FindModel } from '@univerjs/find-replace';
23
+ export declare const DOCS_FIND_REPLACE_PROVIDER = "docs-find-replace-provider";
24
+ export interface IDocFindMatch extends IFindMatch<ITextRange> {
25
+ provider: typeof DOCS_FIND_REPLACE_PROVIDER;
26
+ range: ITextRange;
27
+ replaceable: boolean;
28
+ }
29
+ export declare class DocsFindModel extends FindModel {
30
+ private readonly _doc;
31
+ private readonly _skeletonManager;
32
+ private readonly _selectionManager;
33
+ private readonly _renderManagerService;
34
+ private readonly _commandService;
35
+ private readonly _themeService;
36
+ private readonly _matchesUpdate$;
37
+ readonly matchesUpdate$: import("rxjs").Observable<IDocFindMatch[]>;
38
+ private readonly _activelyChangingMatch$;
39
+ readonly activelyChangingMatch$: import("rxjs").Observable<IDocFindMatch>;
40
+ private _matches;
41
+ private _position;
42
+ private _query;
43
+ private _highlights;
44
+ readonly unitId: string;
45
+ constructor(_doc: DocumentDataModel, _skeletonManager: DocSkeletonManagerService, _selectionManager: DocSelectionManagerService, _renderManagerService: IRenderManagerService, _commandService: ICommandService, _themeService: ThemeService);
46
+ get currentMatch(): Nullable<IDocFindMatch>;
47
+ dispose(): void;
48
+ getMatches(): IDocFindMatch[];
49
+ start(query: IFindQuery): void;
50
+ moveToNextMatch(params?: IFindMoveParams): IDocFindMatch | null;
51
+ moveToPreviousMatch(params?: IFindMoveParams): IDocFindMatch | null;
52
+ replace(replaceString: string): Promise<boolean>;
53
+ replaceAll(replaceString: string): Promise<IReplaceAllResult>;
54
+ focusSelection(): void;
55
+ private _scan;
56
+ private _selectionMatchIndex;
57
+ private _activate;
58
+ private _focusMatch;
59
+ private _refreshHighlights;
60
+ private _disposeHighlights;
61
+ }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Copyright 2023-present DreamNum Co., Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import type { UnitModel } from '@univerjs/core';
17
+ import type { IFindQuery, IFindReplaceProvider } from '@univerjs/find-replace';
18
+ import { Disposable, Injector, IUniverInstanceService } from '@univerjs/core';
19
+ import { IRenderManagerService } from '@univerjs/engine-render';
20
+ import { DocsFindModel } from '../models/docs-find.model';
21
+ export declare class DocsFindReplaceProvider extends Disposable implements IFindReplaceProvider {
22
+ private readonly _instanceService;
23
+ private readonly _renderManagerService;
24
+ private readonly _injector;
25
+ readonly capabilities: {
26
+ caseSensitive: boolean;
27
+ matchesTheWholeWord: boolean;
28
+ matchesTheWholeCell: boolean;
29
+ findDirection: boolean;
30
+ findScope: boolean;
31
+ findBy: boolean;
32
+ };
33
+ private _model;
34
+ constructor(_instanceService: IUniverInstanceService, _renderManagerService: IRenderManagerService, _injector: Injector);
35
+ isSupported(unit: UnitModel): boolean;
36
+ find(query: IFindQuery): Promise<DocsFindModel[]>;
37
+ terminate(): void;
38
+ dispose(): void;
39
+ }
@@ -0,0 +1 @@
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/docs"),require("@univerjs/docs-ui"),require("@univerjs/engine-render"),require("@univerjs/find-replace"),require("@univerjs/ui"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/docs`,`@univerjs/docs-ui`,`@univerjs/engine-render`,`@univerjs/find-replace`,`@univerjs/ui`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverDocsFindReplace={},e.UniverCore,e.UniverDocs,e.UniverDocsUi,e.UniverEngineRender,e.UniverFindReplace,e.UniverUi,e.rxjs))})(this,function(e,t,n,r,i,a,o,s){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var c=`@univerjs/docs-find-replace`,l=`1.0.0-insiders.20260727-46ed76c`;let u={};function d(e,n,r){if(!n.findString)return[];let i=t.regexp.createLiteralRegExp(n.findString,n.caseSensitive?`gu`:`giu`),a=n.matchesTheWholeWord?new Set(Array.from(new Intl.Segmenter(void 0,{granularity:`word`}).segment(e.dataStream)).filter(e=>e.isWordLike).map(e=>`${e.index}:${e.index+e.segment.length}`)):null;return Array.from(e.dataStream.matchAll(i)).filter(e=>{var t;return(t=a==null?void 0:a.has(`${e.index}:${e.index+e[0].length}`))==null||t}).map(t=>{var n,i;let a=t.index,o=a+t[0].length,s=(n=(i=e.customRanges)==null?void 0:i.some(e=>e.wholeEntity&&e.startIndex<o&&e.endIndex+1>a))!=null&&n;return{startOffset:a,endOffset:o,replaceable:!r&&!s}})}let f={id:`docs.command.replace`,type:t.CommandType.COMMAND,handler:(e,r)=>{if(!r)return{success:0,failure:0};let i=e.get(t.IUniverInstanceService),a=e.get(t.ICommandService),o=i.getUnit(r.unitId,t.UniverInstanceType.UNIVER_DOC),s=o==null?void 0:o.getBody();if(!o||!s)return{success:0,failure:+!!r.range};let c=d(s,r.query,!!o.getSnapshot().disabled),l=r.range?c.filter(e=>e.startOffset===r.range.startOffset&&e.endOffset===r.range.endOffset):c,u=l.filter(e=>e.replaceable),f=(r.range&&l.length===0?1:l.length)-u.length;if(!u.length)return{success:0,failure:f};let p=new t.TextX,m=new t.MemoryCursor;for(let e of u){var h,g;let n=o.sliceBody(e.startOffset,e.endOffset),i=r.replaceString?{dataStream:r.replaceString,textRuns:n!=null&&(h=n.textRuns)!=null&&h.length?[{...n.textRuns[0],st:0,ed:r.replaceString.length}]:void 0,customRanges:n!=null&&(g=n.customRanges)!=null&&g.length?[{...n.customRanges[0],startIndex:0,endIndex:r.replaceString.length-1}]:void 0}:null;p.push(...t.BuildTextUtils.selection.delete([{startOffset:e.startOffset,endOffset:e.endOffset,collapsed:!1}],s,m.cursor,i,!1)),m.moveCursorTo(e.endOffset)}let _=t.JSONX.getInstance().editOp(p.serialize(),(0,t.getRichTextEditPath)(o));return a.syncExecuteCommand(n.RichTextEditingMutation.id,{unitId:r.unitId,actions:_,textRanges:[]})?{success:u.length,failure:f}:{success:0,failure:f+u.length}}};function p(e){let n=e.get(t.IContextService);return{id:a.OpenFindDialogOperation.id,icon:`SearchIcon`,tooltip:`find-replace.toolbar`,type:o.MenuItemType.BUTTON,disabled$:n.subscribeContextValue$(t.FOCUSING_DOC).pipe((0,s.map)(e=>!e))}}let m={[o.RibbonStartGroup.OTHERS]:{[a.OpenFindDialogOperation.id]:{order:3,menuItemFactory:p}}};function h(e){"@babel/helpers - typeof";return h=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},h(e)}function g(e,t){if(h(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(h(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function _(e){var t=g(e,`string`);return h(t)==`symbol`?t:t+``}function v(e,t,n){return(t=_(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function y(e,t){return function(n,r){t(n,r,e)}}function b(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}let x=class extends a.FindModel{constructor(e,r,i,a,o,c){super(),this._doc=e,this._skeletonManager=r,this._selectionManager=i,this._renderManagerService=a,this._commandService=o,this._themeService=c,v(this,`_matchesUpdate$`,new s.Subject),v(this,`matchesUpdate$`,this._matchesUpdate$.asObservable()),v(this,`_activelyChangingMatch$`,new s.Subject),v(this,`activelyChangingMatch$`,this._activelyChangingMatch$.asObservable()),v(this,`_matches`,[]),v(this,`_position`,-1),v(this,`_query`,null),v(this,`_highlights`,[]),v(this,`unitId`,void 0),this.unitId=e.getUnitId(),this.disposeWithMe((0,t.toDisposable)((0,t.fromCallback)(this._commandService.onCommandExecuted.bind(this._commandService)).pipe((0,s.filter)(([e])=>{var t;return e.id===n.RichTextEditingMutation.id&&((t=e.params)==null?void 0:t.unitId)===this.unitId}),(0,s.debounceTime)(220)).subscribe(()=>this._scan(!0)))),this.disposeWithMe((0,t.toDisposable)(this._skeletonManager.currentSkeleton$.subscribe(()=>this._refreshHighlights())))}get currentMatch(){var e;return this._position<0||(e=this._matches[this._position])==null?null:e}dispose(){super.dispose(),this._disposeHighlights(),this._matchesUpdate$.complete(),this._activelyChangingMatch$.complete()}getMatches(){return this._matches}start(e){this._query=e,this._position=-1,this._scan(!1)}moveToNextMatch(e){if(!this._matches.length)return null;if(e!=null&&e.stayIfOnMatch&&this.currentMatch)return this._activate(this._position,e.noFocus);let t;if(e!=null&&e.ignoreSelection)t=0;else if(this._position>=0)t=this._position+1;else{let n=this._selectionManager.getActiveTextRange(),r=e!=null&&e.stayIfOnMatch?this._selectionMatchIndex():-1;t=r>=0?r:this._matches.findIndex(e=>{var t;return e.range.startOffset>=((t=n==null?void 0:n.endOffset)==null?0:t)})}return(t<0||t>=this._matches.length)&&(t=e!=null&&e.loop?0:-1),t<0?(this._position=-1,this._refreshHighlights(),null):this._activate(t,e==null?void 0:e.noFocus)}moveToPreviousMatch(e){if(!this._matches.length)return null;if(e!=null&&e.stayIfOnMatch&&this.currentMatch)return this._activate(this._position,e.noFocus);let t;if(e!=null&&e.ignoreSelection)t=this._matches.length-1;else if(this._position>=0)t=this._position-1;else{let n=this._selectionManager.getActiveTextRange(),r=e!=null&&e.stayIfOnMatch?this._selectionMatchIndex():-1;t=r>=0?r:this._matches.findLastIndex(e=>{var t;return e.range.endOffset<=((t=n==null?void 0:n.startOffset)==null?0:t)})}return t<0&&(t=e!=null&&e.loop?this._matches.length-1:-1),t<0?(this._position=-1,this._refreshHighlights(),null):this._activate(t,e==null?void 0:e.noFocus)}async replace(e){let t=this.currentMatch;if(!(t!=null&&t.replaceable)||!this._query)return!1;let n=await this._commandService.executeCommand(f.id,{unitId:this.unitId,query:this._query,replaceString:e,range:t.range});return(n==null?void 0:n.success)===1}async replaceAll(e){var t;return this._query?(t=await this._commandService.executeCommand(f.id,{unitId:this.unitId,query:this._query,replaceString:e}))==null?{success:0,failure:this._matches.length}:t:{success:0,failure:0}}focusSelection(){this.currentMatch&&this._focusMatch(this.currentMatch)}_scan(e){var t;if(!this._query)return;let n=this._doc.getBody();if(!n){this._matches=[],this._position=-1,this._disposeHighlights(),e&&this._matchesUpdate$.next([]);return}let r=(t=this.currentMatch)==null?void 0:t.range.startOffset;this._matches=d(n,this._query,!!this._doc.getSnapshot().disabled).map(e=>({provider:`docs-find-replace-provider`,unitId:this.unitId,range:{...e,collapsed:!1},replaceable:e.replaceable})),this._position=r==null?-1:this._matches.findIndex(e=>e.range.startOffset>=r),this._refreshHighlights(),e&&this._matchesUpdate$.next(this._matches)}_selectionMatchIndex(){let e=this._selectionManager.getActiveTextRange();return e?this._matches.findIndex(t=>t.range.startOffset<=e.startOffset&&t.range.endOffset>=e.endOffset):-1}_activate(e,t=!1){let n=this._matches[e];return n?(this._position=e,t?this._refreshHighlights():this._focusMatch(n),n):null}_focusMatch(e){var t;this._selectionManager.replaceDocRanges([e.range],{unitId:this.unitId,subUnitId:this.unitId},!0,{shouldFocus:!1}),(t=this._renderManagerService.getRenderUnitById(this.unitId))==null||t.with(r.DocBackScrollRenderController).scrollToRange(e.range),this._activelyChangingMatch$.next(e),this._refreshHighlights()}_refreshHighlights(){this._disposeHighlights();let e=this._renderManagerService.getRenderUnitById(this.unitId),n=this._skeletonManager.getSkeleton(),i=e==null?void 0:e.mainComponent;if(!e||!n||!i)return;let a=this._themeService.getColorFromTheme(`yellow.400`),o={strokeWidth:0,stroke:`rgba(0,0,0,0)`,strokeActive:`rgba(0,0,0,0)`,fill:new t.ColorKit(a).setAlpha(.3).toRgbString()},s={...o,fill:new t.ColorKit(a).setAlpha(.65).toRgbString()};this._matches.forEach((t,a)=>{let c=(0,r.getTextRangeFromCharIndex)(t.range.startOffset,t.range.endOffset,e.scene,i,n,a===this._position?s:o,``,-1);c&&this._highlights.push(c)})}_disposeHighlights(){this._highlights.forEach(e=>e.dispose()),this._highlights=[]}};x=b([y(2,(0,t.Inject)(n.DocSelectionManagerService)),y(3,i.IRenderManagerService),y(4,t.ICommandService),y(5,(0,t.Inject)(t.ThemeService))],x);let S=class extends t.Disposable{constructor(e,t,n){super(),this._instanceService=e,this._renderManagerService=t,this._injector=n,v(this,`capabilities`,{caseSensitive:!0,matchesTheWholeWord:!0,matchesTheWholeCell:!1,findDirection:!1,findScope:!1,findBy:!1}),v(this,`_model`,null)}isSupported(e){return e.type===t.UniverInstanceType.UNIVER_DOC}async find(e){var r;this.terminate();let i=this._instanceService.getCurrentUnitOfType(t.UniverInstanceType.UNIVER_DOC);if(!i)return[];let a=(r=this._renderManagerService.getRenderUnitById(i.getUnitId()))==null?void 0:r.with(n.DocSkeletonManagerService);return a?(this._model=this._injector.createInstance(x,i,a),this._model.start(e),[this._model]):[]}terminate(){var e;(e=this._model)==null||e.dispose(),this._model=null}dispose(){this.terminate(),super.dispose()}};S=b([y(0,t.IUniverInstanceService),y(1,i.IRenderManagerService),y(2,(0,t.Inject)(t.Injector))],S);let C=class extends t.Disposable{constructor(e,t,n,r){super(),this.disposeWithMe(e),this.disposeWithMe(t.registerFindReplaceProvider(e)),this.disposeWithMe(n.registerCommand(f)),r.mergeMenu(m)}};C=b([y(0,(0,t.Inject)(S)),y(1,a.IFindReplaceService),y(2,t.ICommandService),y(3,o.IMenuManagerService)],C);let w=class extends t.Plugin{constructor(e=u,n,r){super(),this._config=e,this._injector=n,this._configService=r;let{...i}=(0,t.merge)({},u,this._config);this._configService.setConfig(`docs-find-replace.config`,i)}onStarting(){this._injector.add([S]),this._injector.add([C])}onSteady(){this._injector.get(C)}};v(w,`pluginName`,`DOCS_FIND_REPLACE_PLUGIN`),v(w,`packageName`,c),v(w,`version`,l),v(w,`type`,t.UniverInstanceType.UNIVER_DOC),w=b([(0,t.DependentOn)(n.UniverDocsPlugin,i.UniverRenderEnginePlugin,r.UniverDocsUIPlugin,a.UniverFindReplacePlugin),y(1,(0,t.Inject)(t.Injector)),y(2,t.IConfigService)],w),Object.defineProperty(e,"UniverDocsFindReplacePlugin",{enumerable:!0,get:function(){return w}})});
package/package.json ADDED
@@ -0,0 +1,79 @@
1
+ {
2
+ "name": "@univerjs/docs-find-replace",
3
+ "version": "1.0.0-insiders.20260727-46ed76c",
4
+ "private": false,
5
+ "description": "Find and replace support for Univer Docs.",
6
+ "author": "DreamNum Co., Ltd. <developer@univer.ai>",
7
+ "license": "Apache-2.0",
8
+ "funding": {
9
+ "type": "opencollective",
10
+ "url": "https://opencollective.com/univer"
11
+ },
12
+ "homepage": "https://univer.ai",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/dream-num/univer"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/dream-num/univer/issues"
19
+ },
20
+ "keywords": [
21
+ "univer",
22
+ "docs",
23
+ "find-replace",
24
+ "plugin"
25
+ ],
26
+ "exports": {
27
+ ".": {
28
+ "import": "./lib/es/index.js",
29
+ "require": "./lib/cjs/index.js",
30
+ "types": "./lib/types/index.d.ts"
31
+ },
32
+ "./*": {
33
+ "import": "./lib/es/*",
34
+ "require": "./lib/cjs/*",
35
+ "types": "./lib/types/index.d.ts"
36
+ },
37
+ "./lib/*": "./lib/*"
38
+ },
39
+ "main": "./lib/es/index.js",
40
+ "types": "./lib/types/index.d.ts",
41
+ "publishConfig": {
42
+ "access": "public"
43
+ },
44
+ "directories": {
45
+ "lib": "lib"
46
+ },
47
+ "files": [
48
+ "lib"
49
+ ],
50
+ "peerDependencies": {
51
+ "react": "^16.9.0 || ^17.0.0 || ^18.0.0 || ^19.0.0 || ^19.0.0-rc",
52
+ "rxjs": ">=7.0.0"
53
+ },
54
+ "dependencies": {
55
+ "@univerjs/core": "1.0.0-insiders.20260727-46ed76c",
56
+ "@univerjs/docs-ui": "1.0.0-insiders.20260727-46ed76c",
57
+ "@univerjs/docs": "1.0.0-insiders.20260727-46ed76c",
58
+ "@univerjs/engine-render": "1.0.0-insiders.20260727-46ed76c",
59
+ "@univerjs/find-replace": "1.0.0-insiders.20260727-46ed76c",
60
+ "@univerjs/ui": "1.0.0-insiders.20260727-46ed76c"
61
+ },
62
+ "devDependencies": {
63
+ "react": "18.3.1",
64
+ "rxjs": "^7.8.2",
65
+ "typescript": "^6.0.3",
66
+ "vitest": "^4.1.10",
67
+ "@univerjs-infra/shared": "1.0.0-alpha.7"
68
+ },
69
+ "scripts": {
70
+ "test": "vitest run",
71
+ "test:watch": "vitest",
72
+ "coverage": "vitest run --coverage",
73
+ "typecheck": "tsc --noEmit",
74
+ "build:bundle": "univer-cli build",
75
+ "build:types": "tsc -p tsconfig.node.json",
76
+ "build": "pnpm run build:bundle && pnpm run build:types"
77
+ },
78
+ "module": "./lib/es/index.js"
79
+ }