repo_name string | dataset string | owner string | lang string | func_name string | code string | docstring string | url string | sha string |
|---|---|---|---|---|---|---|---|---|
libro | github_2023 | weavefox | typescript | GenericSearchProvider.highlightPrevious | async highlightPrevious(loop?: boolean): Promise<HTMLSearchMatch | undefined> {
return this._highlightNext(true, loop ?? true) ?? undefined;
} | /**
* Move the current match indicator to the previous match.
*
* @param loop Whether to loop within the matches list.
*
* @returns A promise that resolves once the action has completed.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-generic-provider.ts#L134-L136 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | GenericSearchProvider.replaceCurrentMatch | async replaceCurrentMatch(newText: string, loop?: boolean): Promise<boolean> {
return Promise.resolve(false);
} | /**
* Replace the currently selected match with the provided text
*
* @param newText The replacement text
* @param loop Whether to loop within the matches list.
*
* @returns A promise that resolves with a boolean indicating whether a replace occurred.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-generic-provider.ts#L146-L148 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | GenericSearchProvider.replaceAllMatches | async replaceAllMatches(newText: string): Promise<boolean> {
// This is read only, but we could loosen this in theory for input boxes...
return Promise.resolve(false);
} | /**
* Replace all matches in the notebook with the provided text
*
* @param newText The replacement text
*
* @returns A promise that resolves with a boolean indicating whether a replace occurred.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-generic-provider.ts#L157-L160 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | GenericSearchProvider.startQuery | startQuery = async (query: RegExp | null, filters = {}): Promise<void> => {
this._query = query;
if (query === null) {
await this.endQuery();
return Promise.resolve();
}
const matches = this.view.container?.current
? await searchInHTML(query, this.view.container?.current)
: [];
if (!this.isMatchChanged(this.matches, matches)) {
return Promise.resolve();
}
await this.endQuery();
// Transform the DOM
let nodeIdx = 0;
while (nodeIdx < matches.length) {
const activeNode = matches[nodeIdx].node;
const parent = activeNode.parentNode!;
const subMatches = [matches[nodeIdx]];
while (++nodeIdx < matches.length && matches[nodeIdx].node === activeNode) {
subMatches.unshift(matches[nodeIdx]);
}
const markedNodes = subMatches.map((match) => {
// TODO: support tspan for svg when svg support is added
const markedNode = document.createElement('mark');
markedNode.classList.add(...LIBRO_SEARCH_FOUND_CLASSES);
markedNode.textContent = match.text;
const newNode = activeNode.splitText(match.position);
newNode.textContent = newNode.textContent!.slice(match.text.length);
parent.insertBefore(markedNode, newNode);
return markedNode;
});
// Insert node in reverse order as we replace from last to first
// to maintain match position.
for (let i = markedNodes.length - 1; i >= 0; i--) {
this._markNodes.push(markedNodes[i]);
}
}
if (this.view.container?.current) {
// Watch for future changes:
this._mutationObserver.observe(
this.view.container?.current,
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserverInit
{
attributes: false,
characterData: true,
childList: true,
subtree: true,
},
);
}
this._matches = matches;
} | /**
* Initialize the search using the provided options. Should update the UI
* to highlight all matches and "select" whatever the first match should be.
*
* @param query A RegExp to be use to perform the search
* @param filters Filter parameters to pass to provider
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-generic-provider.ts | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | GenericSearchProvider.endQuery | async endQuery(): Promise<void> {
this._mutationObserver.disconnect();
this._markNodes.forEach((el) => {
const parent = el.parentNode!;
parent.replaceChild(document.createTextNode(el.textContent!), el);
parent.normalize();
});
this._markNodes = [];
this._matches = [];
this._currentMatchIndex = -1;
} | /**
* Clear the highlighted matches and any internal state.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-generic-provider.ts#L253-L263 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.constructor | constructor(searchProvider: SearchProvider, searchDebounceTime: number) {
this.searchProvider = searchProvider;
// this._filters = {};
// if (this.searchProvider.getFilters) {
// const filters = this.searchProvider.getFilters();
// for (const filter in filters) {
// this._filters[filter] = filters[filter].default;
// }
// }
this.toDispose.push(searchProvider.stateChanged(this.refresh));
this.searchDebouncer = debounce(() => {
this.updateSearch().catch((reason) => {
console.error('Failed to update search on document.', reason);
});
}, searchDebounceTime);
} | /**
* Search document model
* @param searchProvider Provider for the current document
* @param searchDebounceTime Debounce search time
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L46-L63 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.caseSensitive | get caseSensitive(): boolean {
return this._caseSensitive;
} | /**
* Whether the search is case sensitive or not.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L68-L70 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.currentIndex | get currentIndex(): number | undefined {
return this.searchProvider.currentMatchIndex;
} | /**
* Current highlighted match index.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L81-L83 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.filters | get filters(): SearchFilters {
return this._filters;
} | /**
* Filter values.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L88-L90 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.filtersDefinition | get filtersDefinition(): Record<string, SearchFilter> {
return this.searchProvider.getFilters?.() ?? {};
} | /**
* Filter definitions for the current provider.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L95-L97 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.initialQuery | get initialQuery(): string {
if (!this.searchProvider.getInitialQuery) {
return this._searchExpression;
}
return this._searchExpression || this.searchProvider.getInitialQuery();
} | /**
* The initial query string.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L102-L107 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.isReadOnly | get isReadOnly(): boolean {
return this.searchProvider.isReadOnly;
} | /**
* Whether the document is read-only or not.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L112-L114 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.replaceText | get replaceText(): string {
return this._replaceText;
} | /**
* Replacement expression
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L119-L121 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.searchExpression | get searchExpression(): string {
return this._searchExpression;
} | /**
* Search expression
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L131-L133 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.totalMatches | get totalMatches(): number | undefined {
return this.searchProvider.matchesCount;
} | /**
* Total number of matches.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L144-L146 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.useRegex | get useRegex(): boolean {
return this._useRegex;
} | /**
* Whether to use regular expression or not.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L151-L153 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.dispose | dispose(): void {
if (this.disposed) {
return;
}
if (this._searchExpression) {
this.endQuery().catch((reason) => {
console.error(`Failed to end query '${this._searchExpression}.`, reason);
});
}
this.toDispose.dispose();
this.searchDebouncer.dispose();
this._disposed = true;
} | /**
* Dispose the model.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L164-L176 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.endQuery | async endQuery(): Promise<void> {
await this.searchProvider.endQuery();
} | /**
* End the query.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L181-L183 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.highlightNext | async highlightNext(): Promise<void> {
await this.searchProvider.highlightNext();
} | /**
* Highlight the next match.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L188-L190 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.highlightPrevious | async highlightPrevious(): Promise<void> {
await this.searchProvider.highlightPrevious();
} | /**
* Highlight the previous match
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L195-L197 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.refresh | refresh(): void {
this.searchDebouncer.invoke().catch((reason: any) => {
console.error('Failed to invoke search document debouncer.', reason);
});
} | /**
* Refresh search
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L202-L206 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.replaceAllMatches | async replaceAllMatches(): Promise<void> {
await this.searchProvider.replaceAllMatches(this._replaceText);
// Emit state change as the index needs to be updated
} | /**
* Replace all matches.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L211-L214 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.replaceCurrentMatch | async replaceCurrentMatch(): Promise<void> {
await this.searchProvider.replaceCurrentMatch(this._replaceText);
} | /**
* Replace the current match.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L219-L221 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchModel.setFilter | async setFilter(name: string, v: boolean): Promise<void> {
// if (this._filters[name] !== v) {
// if (this.searchProvider.validateFilter) {
// this._filters[name] = await this.searchProvider.validateFilter(name, v);
// // If the value was changed
// if (this._filters[name] === v) {
// this.refresh();
// }
// } else {
// this._filters[name] = v;
// this.refresh();
// }
// }
} | /**
* Set the value of a given filter.
*
* @param name Filter name
* @param v Filter value
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-model.ts#L229-L242 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchProvider.constructor | constructor(
@inject(SearchProviderOption) option: SearchProviderOption,
@inject(VirtualizedManagerHelper)
virtualizedManagerHelper: VirtualizedManagerHelper,
) {
super(option);
this.view = option.view as LibroView;
this.virtualizedManager = virtualizedManagerHelper.getOrCreate(this.view.model);
} | /**
* @param option Provide the view to search in
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-provider.ts#L74-L82 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchProvider.isApplicable | static isApplicable(domain: LibroView): domain is LibroView {
// check to see if the CMSearchProvider can search on the
// first cell, false indicates another editor is present
return domain instanceof LibroView;
} | /**
* Report whether or not this provider has the ability to search on the given object
*
* @param domain Widget to test
* @returns Search ability
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-provider.ts#L94-L98 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchProvider.currentMatchIndex | override get currentMatchIndex(): number | undefined {
let agg = 0;
let found = false;
for (let idx = 0; idx < this.searchProviders.length; idx++) {
const provider = this.searchProviders[idx];
const localMatch = provider?.currentMatchIndex;
if (localMatch !== undefined) {
agg += localMatch;
found = true;
break;
} else {
agg += provider?.matchesCount ?? 0;
}
}
return found ? agg : undefined;
} | /**
* The current index of the selected match.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-provider.ts#L103-L118 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchProvider.matchesCount | override get matchesCount(): number | undefined {
const count = this.view.model.cells.reduce((sum, cell) => {
const provider = this.getProvider(cell);
sum += provider?.matchesCount || 0;
return sum;
}, 0);
if (count === 0) {
return undefined;
}
return count;
} | /**
* The number of matches.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-provider.ts#L123-L133 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchProvider.isReadOnly | get isReadOnly(): boolean {
return this.view?.model?.inputEditable ?? false;
} | /**
* Set to true if the widget under search is read-only, false
* if it is editable. Will be used to determine whether to show
* the replace option.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-provider.ts#L140-L142 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchProvider.dispose | override dispose(): void {
if (this.isDisposed) {
return;
}
this.toDispose.dispose();
this.providerMap.clear();
super.dispose();
// const index = this.view.model.active;
this.endQuery()
.then(() => {
if (!this.view.isDisposed) {
// this.view.model.active = index;
// TODO: should active cell?
}
return;
})
.catch((reason) => {
console.error(`Fail to end search query in notebook:\n${reason}`);
});
} | /**
* Dispose of the resources held by the search provider.
*
* #### Notes
* If the object's `dispose` method is called more than once, all
* calls made after the first will be a no-op.
*
* #### Undefined Behavior
* It is undefined behavior to use any functionality of the object
* after it has been disposed unless otherwise explicitly noted.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-provider.ts#L155-L175 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchProvider.getFilters | override getFilters(): Record<string, SearchFilter> {
return {
output: {
title: l10n.t('在 Output 中查找'),
description: l10n.t('在 Output 中查找'),
default: false,
supportReplace: false,
},
selectedCells: {
title: l10n.t('仅在选中 Cell 中查找'),
description: l10n.t('仅在选中 Cell 中查找'),
default: false,
supportReplace: true,
},
};
} | /**
* Get the filters for the given provider.
*
* @returns The filters.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-provider.ts#L182-L197 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchProvider.replaceCurrentMatch | override getInitialQuery = (): string => {
const activeCell = this.view.activeCell;
if (activeCell) {
return this.libroCellSearchProvider.getInitialQuery(activeCell);
}
return '';
} | /**
* Replace the currently selected match with the provided text
*
* @param newText The replacement text.
* @param loop Whether to loop within the matches list.
*
* @returns A promise that resolves with a boolean indicating whether a replace occurred.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-provider.ts | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchUtils.findNext | findNext(
matches: SearchMatch[],
position: number,
lower = 0,
higher = Infinity,
): number | undefined {
let higherBound = Math.min(matches.length - 1, higher);
let lowerBound = lower;
while (lowerBound <= higherBound) {
// 取中间匹配项
const middle = Math.floor(0.5 * (lowerBound + higherBound));
// 中间匹配项的文本偏移量
const currentPosition = matches[middle].position;
if (currentPosition < position) {
// 中间值的偏移量小于查找起点
lowerBound = middle + 1; // 场景1:查找范围下限太小,需要增大,更新中间值
if (lowerBound < matches.length && matches[lowerBound].position > position) {
return lowerBound; // 场景2:下限已经是要查找的前一个匹配项了,下个匹配项的偏移量会大于查找起点。
}
} else if (currentPosition > position) {
// 中间值的偏移量大于查找起点
higherBound = middle - 1; // 场景1:查找范围上限太大,需要减小,更新中间值
if (higherBound > 0 && matches[higherBound].position < position) {
return middle; // 场景2:上限已经是要查找的后一个匹配项了,下个匹配项的偏移量会小于查找起点。
}
} else {
return middle; // 直接命中查找起点,选择此匹配项
}
}
// 查找起点不在 match 范围内,要么在范围前面,要么在范围后面
const first = lowerBound > 0 ? lowerBound - 1 : 0;
const match = matches[first];
return match.position >= position ? first : undefined;
} | /**
* 查找当前 position 最靠近的匹配项,适用于文本中查找
* @param matches 匹配项列表,通常来自一个 cell 或者 output
* @param position 查找起点,当前位置的文本偏移量
* @param lower 查找范围下限,值为匹配列表 index
* @param higher 查找范围上限,值为匹配列表 index
* @returns 下一个匹配项的 index,如果没有找到则返回 null
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-utils.ts#L17-L51 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | LibroSearchUtils.parseQuery | parseQuery(
queryString: string,
caseSensitive: boolean,
regex: boolean,
): RegExp | undefined {
const flag = caseSensitive ? 'g' : 'gi';
// escape regex characters in query if its a string search
const queryText = regex
? queryString
: queryString.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
try {
const ret = new RegExp(queryText, flag);
// If the empty string is hit, the search logic will freeze the browser tab
// Trying /^/ or /$/ on the codemirror search demo, does not find anything.
// So this is a limitation of the editor.
if (ret.test('')) {
return undefined;
}
return ret;
} catch (error) {
return undefined;
}
} | /**
* Build the regular expression to use for searching.
*
* @param queryString Query string
* @param caseSensitive Whether the search is case sensitive or not
* @param regex Whether the expression is a regular expression
* @returns The regular expression to use
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-search/src/libro-search-utils.ts#L61-L84 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YDocument.changed | get changed(): Event<T> {
return this._changed;
} | /**
* The changed signal.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L95-L97 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YDocument.isDisposed | get isDisposed(): boolean {
return this._isDisposed;
} | /**
* Whether the model has been disposed or not.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L102-L104 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YDocument.canUndo | canUndo(): boolean {
return this.undoManager.undoStack.length > 0;
} | /**
* Whether the object can undo changes.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L109-L111 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YDocument.canRedo | canRedo(): boolean {
return this.undoManager.redoStack.length > 0;
} | /**
* Whether the object can redo changes.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L116-L118 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YDocument.dispose | dispose(): void {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
this.ystate.unobserve(this.onStateChanged);
this.awareness.destroy();
this.undoManager.destroy();
this.ydoc.destroy();
} | /**
* Dispose of the resources.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L123-L132 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YDocument.undo | undo(): void {
this.undoManager.undo();
} | /**
* Undo an operation.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L137-L139 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YDocument.redo | redo(): void {
this.undoManager.redo();
} | /**
* Redo an operation.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L144-L146 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YDocument.clearUndoHistory | clearUndoHistory(): void {
this.undoManager.clear();
} | /**
* Clear the change stack.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L151-L153 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YDocument.transact | transact(f: () => void, undoable = true): void {
this.ydoc.transact(f, undoable ? this : null);
} | /**
* Perform a transaction. While the function f is called, all changes to the shared
* document are bundled into a single event.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L159-L161 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YNotebook._onYCellsChanged | protected onStateChanged = (event: Y.YMapEvent<any>): void => {
const stateChange = new Array<StateChange<any>>();
event.keysChanged.forEach((key) => {
const change = event.changes.keys.get(key);
if (change) {
stateChange.push({
name: key,
oldValue: change.oldValue,
newValue: this.ystate.get(key),
});
}
});
this._changedEmitter.fire({ stateChange } as any);
} | /**
* Handle a change to the list of cells.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YFile.create | static create(source?: string): YFile {
const model = new YFile();
if (source) {
model.source = source;
}
return model;
} | /**
* Instantiate a new shareable file.
*
* @param source The initial file content
*
* @returns The file model
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L201-L207 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.source | get source(): string {
return this.getSource();
} | /**
* Cell input content.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L223-L225 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YFile.dispose | override dispose(): void {
if (this.isDisposed) {
return;
}
this.ysource.unobserve(this._modelObserver);
super.dispose();
} | /**
* Dispose of the resources.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L233-L239 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.getSource | getSource(): string {
return this.ysource.toString();
} | /**
* Gets cell's source.
*
* @returns Cell's source.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L246-L248 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YFile.setSource | setSource(value: string): void {
this.transact(() => {
const ytext = this.ysource;
ytext.delete(0, ytext.length);
ytext.insert(0, value);
});
} | /**
* Set the file text.
*
* @param value New text
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L255-L261 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.updateSource | updateSource(start: number, end: number, value = ''): void {
this.transact(() => {
const ysource = this.ysource;
// insert and then delete.
// This ensures that the cursor position is adjusted after the replaced content.
ysource.insert(start, value);
ysource.delete(start + value.length, end - start);
});
} | /**
* Replace content from `start' to `end` with `value`.
*
* @param start: The start index of the range to replace (inclusive).
*
* @param end: The end index of the range to replace (exclusive).
*
* @param value: New source (optional).
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L270-L278 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | createCellModelFromSharedType | const createCellModelFromSharedType = (
type: Y.Map<any>,
options: SharedCell.IOptions = {},
cellTypeAdaptor = defaultCellTypeAdaptor,
): YCellType => {
switch (cellTypeAdaptor(type.get('cell_type'))) {
case 'code':
return new YCodeCell(type, type.get('source'), type.get('outputs'), options);
case 'markdown':
return new YMarkdownCell(type, type.get('source'), options);
case 'raw':
return new YRawCell(type, type.get('source'), options);
default:
throw new Error('Found unknown cell type');
}
}; | /**
* Create a new shared cell model given the YJS shared type.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L294-L309 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | createCell | const createCell = (
cell: SharedCell.Cell,
notebook?: YNotebook,
cellTypeAdaptor = defaultCellTypeAdaptor,
): YCodeCell | YMarkdownCell | YRawCell => {
const ymodel = new Y.Map();
const ysource = new Y.Text();
ymodel.set('source', ysource);
ymodel.set('metadata', {});
ymodel.set('cell_type', cell.cell_type);
ymodel.set('id', cell.id ?? v4());
let ycell: YCellType;
switch (cellTypeAdaptor(cell.cell_type)) {
case 'markdown': {
ycell = new YMarkdownCell(ymodel, ysource, { notebook });
if (cell.attachments !== null) {
ycell.setAttachments(cell.attachments as IAttachments);
}
break;
}
case 'code': {
const youtputs = new Y.Array();
ymodel.set('outputs', youtputs);
ycell = new YCodeCell(ymodel, ysource, youtputs, {
notebook,
});
const cCell = cell as Partial<ICodeCell>;
ycell.execution_count = cCell.execution_count ?? null;
if (cCell.outputs) {
ycell.setOutputs(cCell.outputs);
}
break;
}
default: {
// raw
ycell = new YRawCell(ymodel, ysource, { notebook });
if (cell.attachments) {
ycell.setAttachments(cell.attachments as IAttachments);
}
break;
}
}
if (cell.metadata !== null) {
ycell.setMetadata(cell.metadata);
}
if (cell.source !== null) {
ycell.setSource(
typeof cell.source === 'string' ? cell.source : cell.source.join('\n'),
);
}
return ycell;
}; | /**
* Create a new cell that can be inserted in an existing shared model.
*
* If no notebook is specified the cell will be standalone.
*
* @param cell Cell JSON representation
* @param notebook Notebook to which the cell will be added
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L319-L372 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.createStandalone | static createStandalone(id?: string): YBaseCell<any> {
const cell = createCell({
id,
cell_type: this.prototype.cell_type,
source: '',
metadata: {},
});
return cell;
} | /**
* Create a new YCell that works standalone. It cannot be
* inserted into a YNotebook because the Yjs model is already
* attached to an anonymous Y.Doc instance.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L392-L400 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.constructor | constructor(ymodel: Y.Map<any>, ysource: Y.Text, options: SharedCell.IOptions = {}) {
this.ymodel = ymodel;
this._ysource = ysource;
this._prevSourceLength = ysource ? ysource.length : 0;
this._notebook = null;
this._awareness = null;
this._undoManager = null;
if (options.notebook) {
this._notebook = options.notebook as YNotebook;
// We cannot create a undo manager with the cell not yet attached in the notebook
// so we defer that to the notebook insertCell method
} else {
// Standalone cell
const doc = new Y.Doc();
doc.getArray().insert(0, [this.ymodel]);
this._awareness = new Awareness(doc);
this._undoManager = new Y.UndoManager([this.ymodel], {
trackedOrigins: new Set([this]),
});
}
this.ymodel.observeDeep(this._modelObserver);
} | /**
* Base cell constructor
*
* ### Notes
* Don't use the constructor directly - prefer using ``YNotebook.insertCell``
*
* The ``ysource`` is needed because ``ymodel.get('source')`` will
* not return the real source if the model is not yet attached to
* a document. Requesting it explicitly allows to introspect a non-empty
* source before the cell is attached to the document.
*
* @param ymodel Cell map
* @param ysource Cell source
* @param options { notebook?: The notebook the cell is attached to }
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L417-L439 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.awareness | get awareness(): Awareness | null {
return this._awareness ?? this.notebook?.awareness ?? null;
} | /**
* Cell notebook awareness or null if the cell is standalone.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L444-L446 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.cell_type | get cell_type(): any {
throw new Error('A YBaseCell must not be constructed');
} | /**
* The type of the cell.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L451-L453 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.changed | get changed(): Event<CellChange<Metadata>> {
return this._changed;
} | /**
* The changed signal.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L458-L460 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.id | get id(): string {
return this.getId();
} | /**
* Cell id
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L465-L467 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.isStandalone | get isStandalone(): boolean {
return this._notebook !== null;
} | /**
* Whether the cell is standalone or not.
*
* If the cell is standalone. It cannot be
* inserted into a YNotebook because the Yjs model is already
* attached to an anonymous Y.Doc instance.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L483-L485 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.metadata | get metadata(): Partial<Metadata> {
return this.getMetadata();
} | /**
* Cell metadata.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L490-L492 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.metadataChanged | get metadataChanged(): Event<IMapChange> {
return this._metadataChanged;
} | /**
* Signal triggered when the cell metadata changes.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L500-L502 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.notebook | get notebook(): YNotebook | null {
return this._notebook;
} | /**
* The notebook that this cell belongs to.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L507-L509 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.undoManager | get undoManager(): Y.UndoManager | null {
if (!this.notebook) {
return this._undoManager;
}
return this.notebook?.disableDocumentWideUndoRedo
? this._undoManager
: this.notebook.undoManager;
} | /**
* The cell undo manager.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L524-L531 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.setUndoManager | setUndoManager(): void {
if (this._undoManager) {
throw new Error('The cell undo manager is already set.');
}
if (this._notebook && this._notebook.disableDocumentWideUndoRedo) {
this._undoManager = new Y.UndoManager([this.ymodel], {
trackedOrigins: new Set([this]),
});
}
} | /**
* Defer setting the undo manager as it requires the
* cell to be attached to the notebook Y document.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L537-L547 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.canUndo | canUndo(): boolean {
return !!this.undoManager && this.undoManager.undoStack.length > 0;
} | /**
* Whether the object can undo changes.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L558-L560 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.canRedo | canRedo(): boolean {
return !!this.undoManager && this.undoManager.redoStack.length > 0;
} | /**
* Whether the object can redo changes.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L565-L567 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.clearUndoHistory | clearUndoHistory(): void {
this.undoManager?.clear();
} | /**
* Clear the change stack.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L572-L574 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.undo | undo(): void {
this.undoManager?.undo();
} | /**
* Undo an operation.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L579-L581 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.redo | redo(): void {
this.undoManager?.redo();
} | /**
* Redo an operation.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L586-L588 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.dispose | dispose(): void {
if (this._isDisposed) {
return;
}
this._isDisposed = true;
this.ymodel.unobserveDeep(this._modelObserver);
if (this._awareness) {
// A new document is created for standalone cell.
const doc = this._awareness.doc;
this._awareness.destroy();
doc.destroy();
}
if (this._undoManager) {
// Be sure to not destroy the document undo manager.
if (this._undoManager === this.notebook?.undoManager) {
this._undoManager = null;
} else {
this._undoManager.destroy();
}
}
} | /**
* Dispose of the resources.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L593-L614 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.getId | getId(): string {
return this.ymodel.get('id');
} | /**
* Get cell id.
*
* @returns Cell id
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L621-L623 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.setSource | setSource(value: string): void {
this.transact(() => {
this.ysource.delete(0, this.ysource.length);
this.ysource.insert(0, value);
});
// @todo Do we need proper replace semantic? This leads to issues in editor bindings because they don't switch source.
// this.ymodel.set('source', new Y.Text(value));
} | /**
* Sets cell's source.
*
* @param value: New source.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L639-L646 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.deleteMetadata | deleteMetadata(key: string): void {
const allMetadata = deepCopy(this.ymodel.get('metadata'));
delete allMetadata[key];
this.setMetadata(allMetadata);
} | /**
* Delete a metadata cell.
*
* @param key The key to delete
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L672-L676 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.getMetadata | getMetadata(key?: string): Partial<Metadata> {
const metadata = this.ymodel.get('metadata');
if (typeof key === 'string') {
return deepCopy(metadata[key]);
} else {
return deepCopy(metadata);
}
} | /**
* Returns the metadata associated with the cell.
*
* @param key
* @returns Cell metadata.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L684-L692 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.setMetadata | setMetadata(metadata: Partial<Metadata> | string, value?: PartialJSONValue): void {
if (typeof metadata === 'string') {
if (typeof value === 'undefined') {
throw new TypeError(
`Metadata value for ${metadata} cannot be 'undefined'; use deleteMetadata.`,
);
}
const key = metadata;
// eslint-disable-next-line no-param-reassign
metadata = this.getMetadata();
// @ts-expect-error metadata type is changed at runtime.
metadata[key] = value;
}
const clone = deepCopy(metadata) as any;
if (clone.collapsed !== null) {
clone.jupyter = clone.jupyter || {};
clone.jupyter.outputs_hidden = clone.collapsed;
} else if (clone?.jupyter?.outputs_hidden !== null) {
clone.collapsed = clone.jupyter.outputs_hidden;
}
if (this.ymodel.doc === null || !deepEqual(clone, this.getMetadata())) {
this.transact(() => {
this.ymodel.set('metadata', clone);
});
}
} | /**
* Sets some cell metadata.
*
* If only one argument is provided, it will override all cell metadata.
* Otherwise a single key will be set to a new value.
*
* @param metadata Cell's metadata or key.
* @param value Metadata value
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L703-L729 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.toJSON | toJSON(): IBaseCell {
return {
id: this.getId(),
cell_type: this.cell_type,
source: this.getSource(),
metadata: this.getMetadata(),
};
} | /**
* Serialize the model to JSON.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L734-L741 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.transact | transact(f: () => void, undoable = true): void {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
if (this.notebook && undoable) {
this.notebook.transact(f);
} else {
if (this.ymodel.doc === null) {
f();
} else {
this.ymodel.doc.transact(f, this);
}
}
} | /**
* Perform a transaction. While the function f is called, all changes to the shared
* document are bundled into a single event.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L747-L758 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YBaseCell.getChanges | protected getChanges(events: Y.YEvent<any>[]): Partial<CellChange<Metadata>> {
const changes: CellChange<Metadata> = {};
const sourceEvent = events.find(
(event) => event.target === this.ymodel.get('source'),
);
if (sourceEvent) {
changes.sourceChange = sourceEvent.changes.delta as any;
}
const modelEvent = events.find((event) => event.target === this.ymodel) as
| undefined
| Y.YMapEvent<any>;
if (modelEvent && modelEvent.keysChanged.has('metadata')) {
const change = modelEvent.changes.keys.get('metadata');
const metadataChange = (changes.metadataChange = {
oldValue: change?.oldValue ? change.oldValue : undefined,
newValue: this.getMetadata(),
});
const oldValue = metadataChange.oldValue ?? {};
const oldKeys = Object.keys(oldValue);
const newKeys = Object.keys(metadataChange.newValue);
for (const key of new Set(oldKeys.concat(newKeys))) {
if (!oldKeys.includes(key)) {
this._metadataChangedEmitter.fire({
key,
newValue: metadataChange.newValue[key],
type: 'add',
});
} else if (!newKeys.includes(key)) {
this._metadataChangedEmitter.fire({
key,
oldValue: metadataChange.oldValue[key],
type: 'remove',
});
} else if (!deepEqual(oldValue[key], metadataChange.newValue[key]!)) {
this._metadataChangedEmitter.fire({
key,
newValue: metadataChange.newValue[key],
oldValue: metadataChange.oldValue[key],
type: 'change',
});
}
}
}
// The model allows us to replace the complete source with a new string. We express this in the Delta format
// as a replace of the complete string.
const ysource = this.ymodel.get('source');
if (modelEvent && modelEvent.keysChanged.has('source')) {
changes.sourceChange = [
{ delete: this._prevSourceLength },
{ insert: ysource.toString() },
];
}
this._prevSourceLength = ysource.length;
return changes;
} | /**
* Extract changes from YJS events
*
* @param events YJS events
* @returns Cell changes
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L766-L825 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YCodeCell.createStandalone | static override createStandalone(id?: string): YCodeCell {
return super.createStandalone(id) as YCodeCell;
} | /**
* Create a new YCodeCell that works standalone. It cannot be
* inserted into a YNotebook because the Yjs model is already
* attached to an anonymous Y.Doc instance.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L858-L860 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YCodeCell.constructor | constructor(
ymodel: Y.Map<any>,
ysource: Y.Text,
youtputs: Y.Array<any>,
options: SharedCell.IOptions = {},
) {
super(ymodel, ysource, options);
this._youtputs = youtputs;
} | /**
* Code cell constructor
*
* ### Notes
* Don't use the constructor directly - prefer using ``YNotebook.insertCell``
*
* The ``ysource`` is needed because ``ymodel.get('source')`` will
* not return the real source if the model is not yet attached to
* a document. Requesting it explicitly allows to introspect a non-empty
* source before the cell is attached to the document.
*
* @param ymodel Cell map
* @param ysource Cell source
* @param youtputs Code cell outputs
* @param options { notebook?: The notebook the cell is attached to }
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L878-L886 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YCodeCell.cell_type | override get cell_type(): string {
return this.ymodel.get('cell_type');
} | /**
* The type of the cell.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L891-L893 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YCodeCell.execution_count | get execution_count(): number | null {
return this.ymodel.get('execution_count') || null;
} | /**
* The code cell's prompt number. Will be null if the cell has not been run.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L898-L900 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YCodeCell.outputs | get outputs(): IOutput[] {
return this.getOutputs();
} | /**
* Cell outputs.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L916-L918 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YCodeCell.getOutputs | getOutputs(): IOutput[] {
return deepCopy(this._youtputs.toArray());
} | /**
* Execution, display, or stream outputs.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L926-L928 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YCodeCell.setOutputs | setOutputs(outputs: IOutput[]): void {
this.transact(() => {
this._youtputs.delete(0, this._youtputs.length);
this._youtputs.insert(0, outputs);
}, false);
} | /**
* Replace all outputs.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L933-L938 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YCodeCell.updateOutputs | updateOutputs(start: number, end: number, outputs: IOutput[] = []): void {
const fin =
end < this._youtputs.length ? end - start : this._youtputs.length - start;
this.transact(() => {
this._youtputs.delete(start, fin);
this._youtputs.insert(start, outputs);
}, false);
} | /**
* Replace content from `start' to `end` with `outputs`.
*
* @param start: The start index of the range to replace (inclusive).
*
* @param end: The end index of the range to replace (exclusive).
*
* @param outputs: New outputs (optional).
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L949-L956 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YCodeCell.toJSON | override toJSON(): ICodeCell {
return {
...(super.toJSON() as ICodeCell),
outputs: this.getOutputs(),
execution_count: this.execution_count,
};
} | /**
* Serialize the model to JSON.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L961-L967 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YCodeCell.getChanges | protected override getChanges(
events: Y.YEvent<any>[],
): Partial<CellChange<IBaseCellMetadata>> {
const changes = super.getChanges(events);
const outputEvent = events.find(
(event) => event.target === this.ymodel.get('outputs'),
);
if (outputEvent) {
changes.outputsChange = outputEvent.changes.delta as any;
}
const modelEvent = events.find((event) => event.target === this.ymodel) as
| undefined
| Y.YMapEvent<any>;
if (modelEvent && modelEvent.keysChanged.has('execution_count')) {
const change = modelEvent.changes.keys.get('execution_count');
changes.executionCountChange = {
oldValue: change!.oldValue,
newValue: this.ymodel.get('execution_count'),
};
}
return changes;
} | /**
* Extract changes from YJS events
*
* @param events YJS events
* @returns Cell changes
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L975-L1000 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YAttachmentCell.attachments | get attachments(): IAttachments | undefined {
return this.getAttachments();
} | /**
* Cell attachments
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L1012-L1014 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YAttachmentCell.getAttachments | getAttachments(): IAttachments | undefined {
return this.ymodel.get('attachments');
} | /**
* Gets the cell attachments.
*
* @returns The cell attachments.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L1024-L1026 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YAttachmentCell.setAttachments | setAttachments(attachments: IAttachments | undefined): void {
this.transact(() => {
if (attachments === null) {
this.ymodel.delete('attachments');
} else {
this.ymodel.set('attachments', attachments);
}
});
} | /**
* Sets the cell attachments
*
* @param attachments: The cell attachments.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L1033-L1041 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YAttachmentCell.getChanges | protected override getChanges(
events: Y.YEvent<any>[],
): Partial<CellChange<IBaseCellMetadata>> {
const changes = super.getChanges(events);
const modelEvent = events.find((event) => event.target === this.ymodel) as
| undefined
| Y.YMapEvent<any>;
if (modelEvent && modelEvent.keysChanged.has('attachments')) {
const change = modelEvent.changes.keys.get('attachments');
changes.executionCountChange = {
oldValue: change!.oldValue,
newValue: this.ymodel.get('attachments'),
};
}
return changes;
} | /**
* Extract changes from YJS events
*
* @param events YJS events
* @returns Cell changes
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L1049-L1067 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YRawCell.createStandalone | static override createStandalone(id?: string): YRawCell {
return super.createStandalone(id) as YRawCell;
} | /**
* Create a new YRawCell that works standalone. It cannot be
* inserted into a YNotebook because the Yjs model is already
* attached to an anonymous Y.Doc instance.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L1079-L1081 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YRawCell.cell_type | override get cell_type(): 'raw' {
return 'raw';
} | /**
* String identifying the type of cell.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L1086-L1088 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YRawCell.toJSON | override toJSON(): IRawCell {
return {
id: this.getId(),
cell_type: 'raw',
source: this.getSource(),
metadata: this.getMetadata(),
attachments: this.getAttachments(),
};
} | /**
* Serialize the model to JSON.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L1093-L1101 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YMarkdownCell.createStandalone | static override createStandalone(id?: string): YMarkdownCell {
return super.createStandalone(id) as YMarkdownCell;
} | /**
* Create a new YMarkdownCell that works standalone. It cannot be
* inserted into a YNotebook because the Yjs model is already
* attached to an anonymous Y.Doc instance.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L1113-L1115 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YMarkdownCell.cell_type | override get cell_type(): 'markdown' {
return 'markdown';
} | /**
* String identifying the type of cell.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L1120-L1122 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YMarkdownCell.toJSON | override toJSON(): IMarkdownCell {
return {
id: this.getId(),
cell_type: 'markdown',
source: this.getSource(),
metadata: this.getMetadata(),
attachments: this.getAttachments(),
};
} | /**
* Serialize the model to JSON.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L1127-L1135 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YNotebook.undoChanged | get undoChanged(): Event<boolean> {
return this._undoChangedEmitter.event;
} | /**
* Signal triggered when a undo stack changed.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L1158-L1160 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
libro | github_2023 | weavefox | typescript | YNotebook.redoChanged | get redoChanged(): Event<boolean> {
return this._redoChangedEmitter.event;
} | /**
* Signal triggered when a undo stack changed.
*/ | https://github.com/weavefox/libro/blob/371f9fa4903254d60ed3142e335dbf3f6d8d03e4/packages/libro-shared-model/src/ymodels.ts#L1165-L1167 | 371f9fa4903254d60ed3142e335dbf3f6d8d03e4 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.