Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export const makeDefaultItems = (superToolbar, isDev = false, windowWidth, role,
const renderSearchDropdown = () => {

const handleSubmit = ({ value }) => {
superToolbar.activeEditor.doSearch(value);
superToolbar.activeEditor.commands.search(value);
};

return h('div', {}, [
Expand Down
32 changes: 1 addition & 31 deletions packages/super-editor/src/core/Editor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { EditorState } from 'prosemirror-state';
import { EditorView } from 'prosemirror-view';
import { DOMParser, DOMSerializer } from 'prosemirror-model';
import { search, SearchQuery, setSearchState, getMatchHighlights } from 'prosemirror-search';
import { yXmlFragmentToProseMirrorRootNode } from 'y-prosemirror';
import { helpers } from '@core/index.js';
import { EventEmitter } from './EventEmitter.js';
Expand Down Expand Up @@ -232,30 +231,6 @@ export class Editor extends EventEmitter {
this.options.onFocus({ editor, event });
}

goToFirstMatch() {
const highlights = getMatchHighlights(this.view.state);
if (!highlights || !highlights.children?.length) return;

const match = highlights.children.find(item => item.local);
const firstSearchItemPosition = highlights.children[0] + match.local[0].from + 1;
this.view.domAtPos(firstSearchItemPosition)?.node?.scrollIntoView(true);
}

doSearch(text) {
const query = new SearchQuery({
search: text,
caseSensitive: false,
regexp: false,
wholeWord: false
});
const tr = this.view.state.tr;
setSearchState(tr, query);
this.view.dispatch(tr);

this.goToFirstMatch();
return getMatchHighlights(this.view.state);
}

setToolbar(toolbar) {
this.toolbar = toolbar;
}
Expand Down Expand Up @@ -670,13 +645,8 @@ export class Editor extends EventEmitter {
state: EditorState.create(state),
});

const searchPlugin = search();

const newState = this.state.reconfigure({
plugins: [
...this.extensionService.plugins,
searchPlugin,
],
plugins: [...this.extensionService.plugins],
});

this.view.updateState(newState);
Expand Down
3 changes: 3 additions & 0 deletions packages/super-editor/src/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { PopoverPlugin } from './popover-plugin/index.js';
import { TrackChanges } from './track-changes/index.js';
import { Pagination } from './pagination/index.js';
import { LinkedStyles } from './linked-styles/linked-styles.js';
import { Search } from './search/index.js';

// Helpers
import { trackChangesHelpers } from './track-changes/index.js';
Expand Down Expand Up @@ -161,6 +162,7 @@ const getStarterExtensions = () => {
ShapeContainer,
ShapeTextbox,
ContentBlock,
Search,
];
};

Expand Down Expand Up @@ -220,4 +222,5 @@ export {
AiMark,
AiAnimationMark,
AiPlugin,
Search,
};
1 change: 1 addition & 0 deletions packages/super-editor/src/extensions/search/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './search.js';
45 changes: 45 additions & 0 deletions packages/super-editor/src/extensions/search/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Extension } from '@core/Extension.js';
import { search, SearchQuery, setSearchState, getMatchHighlights } from 'prosemirror-search';


export const Search = Extension.create({
addPmPlugins() {
return [search()];
},

addCommands() {
return {
goToFirstMatch: () => ({ state, editor, dispatch }) => {
const highlights = getMatchHighlights(state);
if (!highlights || !highlights.children?.length) return;

const match = highlights.children.find(item => item.local);
const firstSearchItemPosition = highlights.children[0] + match.local[0].from + 1;
editor.view.domAtPos(firstSearchItemPosition)?.node?.scrollIntoView(true);
},
search: (text) => ({ state, dispatch }) => {
const query = new SearchQuery({
search: text,
caseSensitive: false,
regexp: false,
wholeWord: false,
});
const tr = state.tr;
setSearchState(tr, query);

const newState = state.apply(tr);
const decoSet = getMatchHighlights(newState);
const decorations = decoSet ? decoSet.find() : [];

dispatch(tr);

return decorations.map(deco => ({
from: deco.from,
to: deco.to,
text: newState.doc.textBetween(deco.from, deco.to)
}));
}

}
}
});
4 changes: 4 additions & 0 deletions packages/superdoc/src/core/SuperDoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@ export class SuperDoc extends EventEmitter {
}
}

search(text) {
return this.activeEditor?.commands.search(text);
}

/**
* Set the document to locked or unlocked
* @param {boolean} lock
Expand Down
Loading