Skip to content
Open
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 @@ -241,7 +241,7 @@ function getMoveUpPlacement(
placement = "before";
}
} else if (prevBlock.children.length > 0) {
referenceBlock = prevBlock.children[prevBlock.children.length - 1];
referenceBlock = prevBlock.children.at(-1);
placement = "after";
} else {
referenceBlock = prevBlock;
Expand Down Expand Up @@ -377,8 +377,7 @@ export function moveBlocksDown(
} else {
const selection = editor.getSelection();
sourceBlock =
selection?.blocks[selection?.blocks.length - 1] ||
editor.getTextCursorPosition().block;
selection?.blocks.at(-1) || editor.getTextCursorPosition().block;
}

const moveDownPlacement = getMoveDownPlacement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,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();
});
});
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 @@ -1762,11 +1762,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 @@ -1794,11 +1795,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 @@ -1835,11 +1837,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 @@ -1879,11 +1882,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 @@ -1932,11 +1936,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 @@ -142,10 +142,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 @@ -58,9 +58,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 @@ -81,11 +79,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 @@ -160,8 +157,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 @@ -263,15 +263,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 @@ -293,7 +293,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 @@ -762,7 +762,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
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function calculateListItemIndex(
let isFirst: boolean;

// Determine starting index from the block just before the chain
const lastInChain = chain[chain.length - 1];
const lastInChain = chain.at(-1) ?? { node, pos };
const lastInfo = getBlockInfo({
posBeforeNode: lastInChain.pos,
node: lastInChain.node,
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 @@ -130,9 +130,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 @@ -322,9 +322,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
4 changes: 1 addition & 3 deletions packages/core/src/extensions/SideMenu/SideMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,7 @@ export class SideMenuView<
? 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
2 changes: 1 addition & 1 deletion packages/core/src/extensions/Versioning/Versioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export interface PreviewController<Output = any, Attributions = any> {
export function sortSnapshotsNewestFirst(
snapshots: VersionSnapshot[],
): VersionSnapshot[] {
return [...snapshots].sort((a, b) => b.createdAt - a.createdAt);
return snapshots.toSorted((a, b) => b.createdAt - a.createdAt);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,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 @@ -25,9 +25,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
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const CurrentSnapshot = ({
// "Compare since beginning" diffs the live document against the oldest
// snapshot. Shown only when current-version diffing is supported and there's
// at least one snapshot to compare against.
const oldestSnapshot = snapshots[snapshots.length - 1];
const oldestSnapshot = snapshots.at(-1);
const actions =
comparisonEnabled &&
canPreviewCurrent &&
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)];
}
Loading
Loading