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 @@ -1097,7 +1097,7 @@ describe("Test updateBlock minimal steps", () => {
expect(block.props.level).toBe(3);
// The styled text on either side of the replaced range is preserved.
expect(block.content[0].text).toBe("Paragraph");
expect(block.content[block.content.length - 1].text).toBe("content");
expect(block.content.at(-1)?.text).toBe("content");
expect(() => editor._tiptapEditor.state.doc.check()).not.toThrow();
});
});
Expand Down
25 changes: 15 additions & 10 deletions packages/core/src/api/blockManipulation/tables/tables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1815,11 +1815,12 @@ describe("Test cropEmptyRowsOrColumns", () => {
...simpleTable,
content: {
...simpleTable.content,
rows: simpleTable.content.rows.concat([
rows: [
...simpleTable.content.rows,
{
cells: [emptyCell, emptyCell],
},
]),
],
},
},
"rows",
Expand Down Expand Up @@ -1847,11 +1848,12 @@ describe("Test cropEmptyRowsOrColumns", () => {
...tableWithColspan,
content: {
...tableWithColspan.content,
rows: tableWithColspan.content.rows.concat([
rows: [
...tableWithColspan.content.rows,
{
cells: [emptyCell, emptyCell, emptyCell],
},
]),
],
},
},
"rows",
Expand Down Expand Up @@ -1888,11 +1890,12 @@ describe("Test cropEmptyRowsOrColumns", () => {
...tableWithRowspan,
content: {
...tableWithRowspan.content,
rows: tableWithRowspan.content.rows.concat([
rows: [
...tableWithRowspan.content.rows,
{
cells: [emptyCell, emptyCell, emptyCell],
},
]),
],
},
},
"rows",
Expand Down Expand Up @@ -1932,11 +1935,12 @@ describe("Test cropEmptyRowsOrColumns", () => {
...tableWithColspanAndRowspan,
content: {
...tableWithColspanAndRowspan.content,
rows: tableWithColspanAndRowspan.content.rows.concat([
rows: [
...tableWithColspanAndRowspan.content.rows,
{
cells: [emptyCell, emptyCell, emptyCell],
},
]),
],
},
},
"rows",
Expand Down Expand Up @@ -1985,11 +1989,12 @@ describe("Test cropEmptyRowsOrColumns", () => {
...tableWithComplexRowspansAndColspans,
content: {
...tableWithComplexRowspansAndColspans.content,
rows: tableWithComplexRowspansAndColspans.content.rows.concat([
rows: [
...tableWithComplexRowspansAndColspans.content.rows,
{
cells: [emptyCell, emptyCell, emptyCell, emptyCell],
},
]),
],
},
},
"rows",
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/api/getBlockInfoFromPos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,13 @@ export function getNearestBlockPos(doc: Node, pos: number) {
// eslint-disable-next-line no-console
console.warn(`Position ${pos} is not within a blockContainer node.`);

const resolvedPos = doc.resolve(
allBlockContainerPositions.find((position) => position >= pos) ||
allBlockContainerPositions[allBlockContainerPositions.length - 1],
);
const blockPosition =
allBlockContainerPositions.find((position) => position >= pos) ??
allBlockContainerPositions.at(-1);
if (blockPosition === undefined) {
throw new Error("Document has no block containers");
}
const resolvedPos = doc.resolve(blockPosition);
return {
posBeforeNode: resolvedPos.pos,
node: resolvedPos.nodeAfter!,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/api/getBlocksChangedByTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ function detectReorderedChildren(
}

const lisIndexSet = new Set<number>();
let k = tailsEndsAtIndex[tailsEndsAtIndex.length - 1] ?? -1;
let k = tailsEndsAtIndex.at(-1) ?? -1;
while (k !== -1) {
lisIndexSet.add(k);
k = previousIndexInLis[k];
Expand Down
30 changes: 14 additions & 16 deletions packages/core/src/api/nodeConversions/nodeToBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ export function contentNodeToTableContent<
if (rowIndex === 0) {
rowNode.content.forEach((cellNode) => {
let colWidth = cellNode.attrs.colwidth as null | undefined | number[];
if (colWidth === undefined || colWidth === null) {
colWidth = new Array(cellNode.attrs.colspan ?? 1).fill(undefined);
}
colWidth ??= new Array(cellNode.attrs.colspan ?? 1).fill(undefined);
ret.columnWidths.push(...colWidth);
});
}
Expand All @@ -109,11 +107,10 @@ export function contentNodeToTableContent<
// If the schema only allowed a single tableParagraph node, then the merging would not work and cause prosemirror to fit the content into a new cell
.reduce(
(acc, contentPartial) => {
if (!acc.length) {
const last = acc.at(-1);
if (!last) {
return contentPartial;
}

const last = acc[acc.length - 1];
const first = contentPartial[0];

// Only merge if the last and first content are both styled text nodes and have the same styles
Expand Down Expand Up @@ -194,8 +191,11 @@ export function contentNodeToInlineContent<
currentContent.text += "\n";
} else if (isLinkInlineContent(currentContent)) {
// Current content is a link.
currentContent.content[currentContent.content.length - 1].text +=
"\n";
const lastContent = currentContent.content.at(-1);
if (!lastContent) {
throw new Error("Link content is unexpectedly empty");
}
lastContent.text += "\n";
} else {
throw new Error("unexpected");
}
Expand Down Expand Up @@ -297,15 +297,13 @@ export function contentNodeToInlineContent<
// Node is a link (same type as current content).
// Link URLs are the same.
if (currentContent.href === linkMark.attrs.href) {
const lastContent = currentContent.content.at(-1);
if (!lastContent) {
throw new Error("Link content is unexpectedly empty");
}
// Styles are the same.
if (
JSON.stringify(
currentContent.content[currentContent.content.length - 1]
.styles,
) === JSON.stringify(styles)
) {
currentContent.content[currentContent.content.length - 1].text +=
node.textContent;
if (JSON.stringify(lastContent.styles) === JSON.stringify(styles)) {
lastContent.text += node.textContent;
} else {
// Styles are different.
currentContent.content.push({
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/api/parsers/markdown/markdownToHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ function parseInlineCode(
if (
code.length >= 2 &&
code[0] === " " &&
code[code.length - 1] === " " &&
code.at(-1) === " " &&
/[^ ]/.test(code)
) {
code = code.substring(1, code.length - 1);
Expand Down Expand Up @@ -866,7 +866,7 @@ function tokenize(markdown: string): Token[] {
// Horizontal rule: ---, ***, ___ (3+ chars, optionally with spaces)
if (/^(\s{0,3})([-*_])\s*(\2\s*){2,}$/.test(line)) {
// Setext H2: --- immediately after a paragraph (no blank line between)
const prevToken = tokens[tokens.length - 1];
const prevToken = tokens.at(-1);
if (
!prevLineWasBlank &&
line.trim().match(/^-+$/) &&
Expand Down
10 changes: 7 additions & 3 deletions packages/core/src/comments/threadstore/TipTapThreadStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ export class TiptapThreadStore extends ThreadStore {
},
});

return this.tiptapCommentToCommentData(
thread.comments[thread.comments.length - 1],
);
const lastComment = thread.comments.at(-1);
if (!lastComment) {
throw new Error(
"Comment provider returned an empty thread after adding a comment",
);
}
return this.tiptapCommentToCommentData(lastComment);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/editor/managers/BlockManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ export class BlockManager<
return false;
}

const children = reverse
? block.children.slice().reverse()
: block.children;
const children = reverse ? block.children.toReversed() : block.children;

if (!traverseBlockArray(children)) {
return false;
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/editor/managers/ExtensionManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,9 @@ export class ExtensionManager {
}

// ---- Add phase (no updatePlugins call) ----
const newExtensions = ([] as (Extension | ExtensionFactoryInstance)[])
.concat(toRegister)
.filter(Boolean) as (Extension | ExtensionFactoryInstance)[];
const newExtensions = (
Array.isArray(toRegister) ? toRegister : [toRegister]
).filter(Boolean) as (Extension | ExtensionFactoryInstance)[];

const registeredExtensions = newExtensions
.map((ext) => this.addExtension(ext))
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/editor/transformPasted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,9 @@ export function wrapTableRows(f: Fragment, schema: Schema) {
const newItems: any[] = [];
for (let i = 0; i < f.childCount; i++) {
if (f.child(i).type.name === "tableRow") {
if (
newItems.length > 0 &&
newItems[newItems.length - 1].type.name === "table"
) {
const prevTable = newItems.at(-1);
if (prevTable?.type.name === "table") {
// append to existing table
const prevTable = newItems[newItems.length - 1];
const newTable = prevTable.copy(prevTable.content.addToEnd(f.child(i)));
newItems[newItems.length - 1] = newTable;
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/extensions/SideMenu/blockDropHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function findDropEditor(
? coords.clientY - rect.bottom
: 0;

const distance = Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));
const distance = Math.sqrt(distanceX ** 2 + distanceY ** 2);

if (distance < minDistance) {
minDistance = distance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ export function autolink(options: AutolinkOptions): Plugin {
return;
}

const lastWordBeforeSpace =
wordsBeforeWhitespace[wordsBeforeWhitespace.length - 1];
const lastWordBeforeSpace = wordsBeforeWhitespace.at(-1) ?? "";
const lastWordAndBlockOffset =
textBlock.pos +
textBeforeWhitespace.lastIndexOf(lastWordBeforeSpace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function countChar(str: string, ch: string): number {
*/
function extractTld(hostname: string): string {
const parts = hostname.split(".");
return parts[parts.length - 1].toLowerCase();
return (parts.at(-1) ?? "").toLowerCase();
}

function isValidTld(hostname: string): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ function removeDuplicates(array: any, by = JSON.stringify) {
const seen: any = {};
return array.filter((item: any) => {
const key = by(item);
return Object.prototype.hasOwnProperty.call(seen, key)
? false
: (seen[key] = true);
return Object.hasOwn(seen, key) ? false : (seen[key] = true);
});
}

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/util/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export function filenameFromURL(url: string): string {
const parts = url.split("/");
if (
!parts.length || // invalid?
parts[parts.length - 1] === "" // for example, URL ends in a directory-like trailing slash
parts.at(-1) === "" // for example, URL ends in a directory-like trailing slash
) {
// in this case just return the original url
return url;
}
return parts[parts.length - 1];
return parts.at(-1) ?? url;
}

export function isVideoUrl(url: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/y/extensions/YCursorPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function isDarkColor(bgColor: string): boolean {
if (col <= 0.03928) {
return col / 12.92;
}
return Math.pow((col + 0.055) / 1.055, 2.4);
return ((col + 0.055) / 1.055) ** 2.4;
});
const L = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
return L <= 0.179;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/yjs/extensions/YCursorPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function isDarkColor(bgColor: string): boolean {
if (col <= 0.03928) {
return col / 12.92;
}
return Math.pow((col + 0.055) / 1.055, 2.4);
return ((col + 0.055) / 1.055) ** 2.4;
});
const L = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
return L <= 0.179;
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/components/Comments/ThreadsSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ function sortThreads(
// sort by latest comment in thread first
return threads.sort(
(a, b) =>
b.comments[b.comments.length - 1].createdAt.getTime() -
a.comments[a.comments.length - 1].createdAt.getTime(),
(b.comments.at(-1)?.createdAt ?? b.createdAt).getTime() -
(a.comments.at(-1)?.createdAt ?? a.createdAt).getTime(),
);
}

Expand Down
6 changes: 2 additions & 4 deletions packages/react/src/hooks/useStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,11 @@ export function shallow<T>(objA: T, objB: T): boolean {

return keysA.every(
(key) =>
Object.prototype.hasOwnProperty.call(objB, key) &&
Object.hasOwn(objB, key) &&
Object.is(objA[key as keyof T], objB[key as keyof T]),
);
}

function getOwnKeys<T extends object>(obj: T): Array<string | symbol> {
return (Object.keys(obj) as Array<string | symbol>).concat(
Object.getOwnPropertySymbols(obj),
);
return [...Object.keys(obj), ...Object.getOwnPropertySymbols(obj)];
}
3 changes: 1 addition & 2 deletions packages/xl-ai-server/src/routes/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ const MANUAL_TOOL_CALLS = [
// Use `streamText` to stream text responses from the LLM
regularRoute.post("/streamText", async (c) => {
const { messages } = await c.req.json();
const blockId =
messages[messages.length - 1].metadata.documentState.blocks[0].id;
const blockId = messages.at(-1).metadata.documentState.blocks[0].id;

MANUAL_TOOL_CALLS[0].args.operations[0].referenceId = blockId;
const stream = createUIMessageStream<any>({
Expand Down
5 changes: 2 additions & 3 deletions packages/xl-ai-server/src/routes/serverPersistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ serverPersistenceRoute.post("/streamText", async (c) => {
);

// get results from tool parts to update the last message
if (messages.length > 0 && toolParts.length > 0) {
const lastMessage = messages[messages.length - 1];

const lastMessage = messages.at(-1);
if (lastMessage && toolParts.length > 0) {
messages[messages.length - 1] = {
...lastMessage,
parts: lastMessage.parts.map((part) => {
Expand Down
4 changes: 0 additions & 4 deletions packages/xl-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@
"@handlewithcare/prosemirror-suggest-changes": "^0.1.8",
"@tiptap/core": "^3.31.3",
"ai": "^6.0.5",
"lodash.isequal": "^4.5.0",
"lodash.merge": "^4.6.2",
"prosemirror-changeset": "^2.4.2",
"prosemirror-model": "^1.25.11",
"prosemirror-state": "^1.4.4",
Expand All @@ -114,8 +112,6 @@
"@types/diff": "^6.0.0",
"@types/json-diff": "^1.0.3",
"@types/json-schema": "^7.0.15",
"@types/lodash.isequal": "^4.5.8",
"@types/lodash.merge": "^4.6.9",
"@types/node": "22.13.13",
"@types/react": "^19.2.3",
"@types/react-dom": "^19.2.3",
Expand Down
Loading
Loading