-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add composer spoiler formatting #1055
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d37cc0b
Add composer spoiler formatting
klopez4212 6aa2523
Disable spoiler toggle during uploads
klopez4212 3368cb0
Merge remote-tracking branch 'origin/main' into kennylopez-spoiler-fo…
klopez4212 0d11c29
Address spoiler review feedback
klopez4212 4ab66d6
Fix spoiler toggles around code blocks
klopez4212 7145ad1
Fix block spoiler attachment handling
klopez4212 4d9215e
Respect non-interactive spoiler previews
klopez4212 30da3af
Parse spoilers inside link labels
klopez4212 7b3479d
Keep spoilers masked until reveal
klopez4212 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
desktop/src/features/messages/lib/spoilerFormatting.test.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
|
|
||
| import { getSchema } from "@tiptap/core"; | ||
| import StarterKit from "@tiptap/starter-kit"; | ||
|
|
||
| import { getSpoilerRangeState } from "./spoilerFormatting.ts"; | ||
| import { SpoilerMark, SPOILER_MARK_NAME } from "./spoilerMark.ts"; | ||
|
|
||
| const schema = getSchema([ | ||
| StarterKit.configure({ | ||
| hardBreak: { keepMarks: true }, | ||
| heading: false, | ||
| trailingNode: false, | ||
| link: false, | ||
| }), | ||
| SpoilerMark, | ||
| ]); | ||
|
|
||
| const spoilerMark = schema.marks[SPOILER_MARK_NAME]; | ||
| const codeMark = schema.marks.code; | ||
| const para = (...content) => schema.nodes.paragraph.create(null, content); | ||
| const codeBlock = (content) => schema.nodes.codeBlock.create(null, content); | ||
| const t = (text, marks = []) => schema.text(text, marks); | ||
|
|
||
| function doc(...content) { | ||
| return schema.nodes.doc.create(null, content); | ||
| } | ||
|
|
||
| function wholeDocState(d) { | ||
| return getSpoilerRangeState(d, spoilerMark, 1, d.content.size - 1); | ||
| } | ||
|
|
||
| test("getSpoilerRangeState: ignores inline code when surrounding text is spoilered", () => { | ||
| const d = doc( | ||
| para( | ||
| t("hidden ", [spoilerMark.create()]), | ||
| t("literal", [codeMark.create()]), | ||
| t(" text", [spoilerMark.create()]), | ||
| ), | ||
| ); | ||
|
|
||
| assert.equal(wholeDocState(d), "fully-spoiled"); | ||
| }); | ||
|
|
||
| test("getSpoilerRangeState: ignores code blocks when surrounding text is spoilered", () => { | ||
| const d = doc( | ||
| para(t("hidden", [spoilerMark.create()])), | ||
| codeBlock(t("const secret = true;")), | ||
| ); | ||
|
|
||
| assert.equal(wholeDocState(d), "fully-spoiled"); | ||
| }); | ||
|
|
||
| test("getSpoilerRangeState: reports unspoilered markable text as partial", () => { | ||
| const d = doc( | ||
| para(t("hidden", [spoilerMark.create()])), | ||
| codeBlock(t("const secret = true;")), | ||
| para(t("visible")), | ||
| ); | ||
|
|
||
| assert.equal(wholeDocState(d), "partially-spoiled"); | ||
| }); | ||
|
|
||
| test("getSpoilerRangeState: returns no markable content for code-only ranges", () => { | ||
| const d = doc(codeBlock(t("const secret = true;"))); | ||
|
|
||
| assert.equal(wholeDocState(d), "no-markable-content"); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import type { Editor } from "@tiptap/react"; | ||
| import type { MarkType, Node as ProseMirrorNode } from "@tiptap/pm/model"; | ||
|
|
||
| import { SPOILER_MARK_NAME } from "./spoilerMark"; | ||
|
|
||
| export type SpoilerRangeState = | ||
| | "fully-spoiled" | ||
| | "partially-spoiled" | ||
| | "no-markable-content"; | ||
|
|
||
| function canTextNodeHoldMark( | ||
| node: ProseMirrorNode, | ||
| parent: ProseMirrorNode | null, | ||
| markType: MarkType, | ||
| ): boolean { | ||
| if (!node.isText || node.textContent.length === 0) return false; | ||
| if (parent && !parent.type.allowsMarkType(markType)) return false; | ||
| if (markType.isInSet(node.marks)) return true; | ||
|
|
||
| return node.marks.every((mark) => !mark.type.excludes(markType)); | ||
| } | ||
|
|
||
| export function getSpoilerRangeState( | ||
| doc: ProseMirrorNode, | ||
| spoilerMark: MarkType, | ||
| from: number, | ||
| to: number, | ||
| ): SpoilerRangeState { | ||
| let hasMarkableContent = false; | ||
| let isFullySpoiled = true; | ||
|
|
||
| doc.nodesBetween(from, to, (node, _pos, parent) => { | ||
| if (!canTextNodeHoldMark(node, parent, spoilerMark)) return; | ||
|
|
||
| hasMarkableContent = true; | ||
| if (!spoilerMark.isInSet(node.marks)) { | ||
| isFullySpoiled = false; | ||
| return false; | ||
| } | ||
| }); | ||
|
|
||
| if (!hasMarkableContent) return "no-markable-content"; | ||
| return isFullySpoiled ? "fully-spoiled" : "partially-spoiled"; | ||
| } | ||
|
|
||
| export function getEditorSpoilerRangeState( | ||
| editor: Editor, | ||
| from: number, | ||
| to: number, | ||
| ): SpoilerRangeState { | ||
| const spoilerMark = editor.schema.marks[SPOILER_MARK_NAME]; | ||
| if (!spoilerMark) return "no-markable-content"; | ||
|
|
||
| return getSpoilerRangeState(editor.state.doc, spoilerMark, from, to); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
|
|
||
| import { findClosingDelimiter } from "./spoilerMark.ts"; | ||
|
|
||
| test("findClosingDelimiter: closes on the first inner delimiter", () => { | ||
| const source = "||a || b||"; | ||
|
|
||
| assert.equal(findClosingDelimiter(source, 2, source.length), 4); | ||
| }); | ||
|
|
||
| test("findClosingDelimiter: returns -1 when no closing delimiter exists", () => { | ||
| const source = "||open spoiler"; | ||
|
|
||
| assert.equal(findClosingDelimiter(source, 2, source.length), -1); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.