-
-
Notifications
You must be signed in to change notification settings - Fork 504
feat: add support for diff rendering, apply and reject #2312
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 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7b1adb4
feat: add support for diff rendering, apply and reject
Saul-Mirone fe5dec4
fix: improve
Saul-Mirone 07fbf96
chore: f
Saul-Mirone 7b3b706
chore: f
Saul-Mirone c822bcf
chore: f
Saul-Mirone c4d07ca
chore: f
Saul-Mirone 81d92f7
docs: add doc
Saul-Mirone f87168d
chore: f
Saul-Mirone 225b57a
fix: doc
Saul-Mirone 0306d1b
chore: improve
Saul-Mirone bb51608
chore: f
Saul-Mirone a30e854
fix: test name
Saul-Mirone 59e98f4
test: add tests
Saul-Mirone 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <title>Crepe Diff</title> | ||
| </head> | ||
| <body> | ||
| <div id="app"></div> | ||
| <script type="module" src="/crepe-diff/main.ts"></script> | ||
| </body> | ||
| </html> |
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,4 @@ | ||
| export const crepeDiff = { | ||
| title: 'Crepe Diff', | ||
| link: '/crepe-diff/', | ||
| } |
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,34 @@ | ||
| import { Crepe, CrepeFeature } from '@milkdown/crepe' | ||
| import '@milkdown/crepe/theme/common/style.css' | ||
| import '@milkdown/crepe/theme/frame.css' | ||
| import { callCommand } from '@milkdown/utils' | ||
|
|
||
| import { setup } from '../utils' | ||
|
|
||
| // Command names match the string IDs registered by $command() in plugin-diff. | ||
| // Using strings here because @milkdown/kit is not a direct dependency of e2e. | ||
| setup(async () => { | ||
| const crepe = new Crepe({ | ||
| root: '#app', | ||
| features: { | ||
| [CrepeFeature.Diff]: true, | ||
| }, | ||
| }) | ||
| globalThis.__crepe__ = crepe | ||
| await crepe.create() | ||
|
|
||
| globalThis.__applyDiff__ = (markdown: string) => | ||
| crepe.editor.action(callCommand('StartDiffReview', markdown)) | ||
| globalThis.__acceptAll__ = () => | ||
| crepe.editor.action(callCommand('AcceptAllDiffs')) | ||
| globalThis.__rejectAll__ = () => | ||
| crepe.editor.action(callCommand('RejectAllDiffs')) | ||
| globalThis.__clearDiff__ = () => | ||
| crepe.editor.action(callCommand('ClearDiffReview')) | ||
| globalThis.__acceptChunk__ = (index: number) => | ||
| crepe.editor.action(callCommand('AcceptDiffChunk', index)) | ||
| globalThis.__rejectChunk__ = (index: number) => | ||
| crepe.editor.action(callCommand('RejectDiffChunk', index)) | ||
|
|
||
| return crepe.editor | ||
| }).catch(console.error) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,260 @@ | ||
| import { expect, test } from '@playwright/test' | ||
|
|
||
| import { getMarkdown, setMarkdown, waitNextFrame } from '../misc' | ||
|
|
||
| test.beforeEach(async ({ page }) => { | ||
| await page.goto('/crepe-diff/') | ||
| }) | ||
|
|
||
| async function applyDiff(page: any, original: string, modified: string) { | ||
| await setMarkdown(page, original) | ||
| await waitNextFrame(page) | ||
| await page.evaluate((md: string) => window.__applyDiff__(md), modified) | ||
| await waitNextFrame(page) | ||
| } | ||
|
|
||
| test.describe('diff commands', () => { | ||
| const original = `# Hello World\n\nFirst paragraph.` | ||
| const modified = `# Hello Milkdown\n\nUpdated paragraph.` | ||
|
|
||
| test('accept all applies the new document', async ({ page }) => { | ||
| await applyDiff(page, original, modified) | ||
|
|
||
| await page.evaluate(() => window.__acceptAll__()) | ||
| await waitNextFrame(page) | ||
|
|
||
| const markdown = await getMarkdown(page) | ||
| expect(markdown).toContain('Hello Milkdown') | ||
| expect(markdown).not.toContain('Hello World') | ||
| }) | ||
|
|
||
| test('reject all keeps the original document', async ({ page }) => { | ||
| await applyDiff(page, original, modified) | ||
|
|
||
| await page.evaluate(() => window.__rejectAll__()) | ||
| await waitNextFrame(page) | ||
|
|
||
| const markdown = await getMarkdown(page) | ||
| expect(markdown).toContain('Hello World') | ||
| expect(markdown).not.toContain('Hello Milkdown') | ||
| }) | ||
|
|
||
| test('clear diff removes all decorations', async ({ page }) => { | ||
| await applyDiff(page, original, modified) | ||
|
|
||
| const editor = page.locator('.editor') | ||
| await expect(editor.locator('.milkdown-diff-added').first()).toBeVisible() | ||
|
|
||
| await page.evaluate(() => window.__clearDiff__()) | ||
| await waitNextFrame(page) | ||
|
|
||
| await expect(editor.locator('.milkdown-diff-added')).toHaveCount(0) | ||
| await expect(editor.locator('.milkdown-diff-controls')).toHaveCount(0) | ||
| }) | ||
|
|
||
| test('accept chunk reduces pending changes', async ({ page }) => { | ||
| await applyDiff(page, original, modified) | ||
|
|
||
| const editor = page.locator('.editor') | ||
| const before = await editor | ||
| .locator('.milkdown-diff-controls, .milkdown-diff-controls-block') | ||
| .count() | ||
|
|
||
| await page.evaluate(() => window.__acceptChunk__(0)) | ||
| await waitNextFrame(page) | ||
|
|
||
| const after = await editor | ||
| .locator('.milkdown-diff-controls, .milkdown-diff-controls-block') | ||
| .count() | ||
| expect(after).toBeLessThan(before) | ||
| }) | ||
|
|
||
| test('reject chunk reduces pending changes', async ({ page }) => { | ||
| await applyDiff(page, original, modified) | ||
|
|
||
| const editor = page.locator('.editor') | ||
| const before = await editor | ||
| .locator('.milkdown-diff-controls, .milkdown-diff-controls-block') | ||
| .count() | ||
|
|
||
| await page.evaluate(() => window.__rejectChunk__(0)) | ||
| await waitNextFrame(page) | ||
|
|
||
| const after = await editor | ||
| .locator('.milkdown-diff-controls, .milkdown-diff-controls-block') | ||
| .count() | ||
| expect(after).toBeLessThan(before) | ||
| }) | ||
|
|
||
| test('lock on review prevents document editing', async ({ page }) => { | ||
| await applyDiff(page, original, modified) | ||
|
|
||
| const editor = page.locator('.editor') | ||
| await editor.click() | ||
| await page.keyboard.type('SHOULD NOT APPEAR') | ||
| await waitNextFrame(page) | ||
|
|
||
| const markdown = await getMarkdown(page) | ||
| expect(markdown).not.toContain('SHOULD NOT APPEAR') | ||
| }) | ||
| }) | ||
|
|
||
| test.describe('inline diff', () => { | ||
| test('text change within a paragraph shows inline decorations', async ({ | ||
| page, | ||
| }) => { | ||
| await applyDiff( | ||
| page, | ||
| 'Hello world, this is a test.', | ||
| 'Hello milkdown, this is a test.' | ||
| ) | ||
|
|
||
| const editor = page.locator('.editor') | ||
| const removed = editor.locator('.milkdown-diff-removed') | ||
| const added = editor.locator('.milkdown-diff-added') | ||
| await expect(removed.first()).toBeVisible() | ||
| await expect(added.first()).toBeVisible() | ||
| }) | ||
|
|
||
| test('format change with text difference shows diff', async ({ page }) => { | ||
| await applyDiff( | ||
| page, | ||
| 'This is **bold** and normal text.', | ||
| 'This is **bold** and *italic* text.' | ||
| ) | ||
|
|
||
| const editor = page.locator('.editor') | ||
| const added = editor.locator('.milkdown-diff-added') | ||
| await expect(added.first()).toBeVisible() | ||
|
|
||
| await page.evaluate(() => window.__acceptAll__()) | ||
| await waitNextFrame(page) | ||
|
|
||
| const markdown = await getMarkdown(page) | ||
| expect(markdown).toContain('*italic*') | ||
| }) | ||
|
|
||
| test('accept inline diff updates the text', async ({ page }) => { | ||
| await applyDiff(page, 'Hello world.', 'Hello milkdown.') | ||
|
|
||
| await page.evaluate(() => window.__acceptAll__()) | ||
| await waitNextFrame(page) | ||
|
|
||
| const markdown = await getMarkdown(page) | ||
| expect(markdown).toContain('milkdown') | ||
| expect(markdown).not.toContain('world') | ||
| }) | ||
| }) | ||
|
|
||
| test.describe('block diff', () => { | ||
| test('new paragraph shows as block insertion', async ({ page }) => { | ||
| await applyDiff( | ||
| page, | ||
| '# Heading\n\nFirst paragraph.', | ||
| '# Heading\n\nFirst paragraph.\n\nSecond paragraph.' | ||
| ) | ||
|
|
||
| const editor = page.locator('.editor') | ||
| const addedBlock = editor.locator('.milkdown-diff-added-block') | ||
| await expect(addedBlock.first()).toBeVisible() | ||
| }) | ||
|
|
||
| test('deleted heading shows strikethrough', async ({ page }) => { | ||
| await applyDiff( | ||
| page, | ||
| '# First\n\n## Second\n\nParagraph.', | ||
| '# First\n\nParagraph.' | ||
| ) | ||
|
|
||
| const editor = page.locator('.editor') | ||
| const removed = editor.locator('.milkdown-diff-removed') | ||
| await expect(removed.first()).toBeVisible() | ||
| }) | ||
|
|
||
| test('new list item shows as block insertion', async ({ page }) => { | ||
| await applyDiff(page, '- Item 1\n- Item 2', '- Item 1\n- Item 2\n- Item 3') | ||
|
|
||
| const editor = page.locator('.editor') | ||
| const addedBlock = editor.locator('.milkdown-diff-added-block') | ||
| await expect(addedBlock.first()).toBeVisible() | ||
|
|
||
| await page.evaluate(() => window.__acceptAll__()) | ||
| await waitNextFrame(page) | ||
|
|
||
| const markdown = await getMarkdown(page) | ||
| expect(markdown).toContain('Item 3') | ||
| }) | ||
| }) | ||
|
|
||
| test.describe('image diff', () => { | ||
| test('image src change shows new image widget and marks old as removed', async ({ | ||
| page, | ||
| }) => { | ||
| await applyDiff(page, '![]()', '') | ||
|
|
||
| const editor = page.locator('.editor') | ||
| // Old image-block should have removed-block decoration | ||
| const removedBlock = editor.locator('.milkdown-diff-removed-block') | ||
| await expect(removedBlock.first()).toBeVisible() | ||
|
|
||
| // New image should appear in the added widget | ||
| const addedBlock = editor.locator('.milkdown-diff-added-block') | ||
| await expect(addedBlock.first()).toBeVisible() | ||
| }) | ||
|
|
||
| test('accept image diff updates the document', async ({ page }) => { | ||
| await applyDiff(page, '![]()', '') | ||
|
|
||
| await page.evaluate(() => window.__acceptAll__()) | ||
| await waitNextFrame(page) | ||
|
|
||
| const markdown = await getMarkdown(page) | ||
| expect(markdown).toContain('example.com/new.png') | ||
| }) | ||
| }) | ||
|
|
||
| test.describe('table diff', () => { | ||
| test('table with new column shows as block replacement', async ({ page }) => { | ||
| await applyDiff( | ||
| page, | ||
| '| A | B |\n| - | - |\n| 1 | 2 |', | ||
| '| A | B | C |\n| - | - | - |\n| 1 | 2 | 3 |' | ||
| ) | ||
|
|
||
| const editor = page.locator('.editor') | ||
| // Old table should be marked as removed block | ||
| const removedBlock = editor.locator('.milkdown-diff-removed-block') | ||
| await expect(removedBlock.first()).toBeVisible() | ||
|
|
||
| // New table should appear in the added widget | ||
| const addedBlock = editor.locator('.milkdown-diff-added-block') | ||
| await expect(addedBlock.first()).toBeVisible() | ||
| }) | ||
|
|
||
| test('table with new row shows as block replacement', async ({ page }) => { | ||
| await applyDiff( | ||
| page, | ||
| '| A | B |\n| - | - |\n| 1 | 2 |', | ||
| '| A | B |\n| - | - |\n| 1 | 2 |\n| 3 | 4 |' | ||
| ) | ||
|
|
||
| const editor = page.locator('.editor') | ||
| const addedBlock = editor.locator('.milkdown-diff-added-block') | ||
| await expect(addedBlock.first()).toBeVisible() | ||
| }) | ||
|
|
||
| test('accept table diff updates to new table', async ({ page }) => { | ||
| await applyDiff( | ||
| page, | ||
| '| A | B |\n| - | - |\n| 1 | 2 |', | ||
| '| A | B | C |\n| - | - | - |\n| 1 | 2 | 3 |' | ||
| ) | ||
|
|
||
| await page.evaluate(() => window.__acceptAll__()) | ||
| await waitNextFrame(page) | ||
|
|
||
| const markdown = await getMarkdown(page) | ||
| expect(markdown).toContain('C') | ||
| expect(markdown).toContain('3') | ||
| }) | ||
| }) | ||
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
Oops, something went wrong.
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.