-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #740 from streamich/peritext-event-improvements
Peritext `insert` and `delete` events
- Loading branch information
Showing
7 changed files
with
380 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
228 changes: 228 additions & 0 deletions
228
src/json-crdt-peritext-ui/events/__tests__/delete.spec.ts
This file contains 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,228 @@ | ||
import {type Kit, runAlphabetKitTestSuite} from '../../../json-crdt-extensions/peritext/__tests__/setup'; | ||
import {PeritextEventDefaults} from '../PeritextEventDefaults'; | ||
import {PeritextEventTarget} from '../PeritextEventTarget'; | ||
|
||
const testSuite = (getKit: () => Kit) => { | ||
const setup = () => { | ||
const kit = getKit(); | ||
const et = new PeritextEventTarget(); | ||
const defaults = new PeritextEventDefaults(kit.peritext, et); | ||
et.defaults = defaults; | ||
return {...kit, et}; | ||
}; | ||
|
||
test('can delete by a single character backwards', async () => { | ||
const kit = setup(); | ||
kit.editor.cursor.setAt(10); | ||
expect(kit.peritext.str.view()).toBe('abcdefghijklmnopqrstuvwxyz'); | ||
kit.et.delete(-1); | ||
expect(kit.peritext.str.view()).toBe('abcdefghiklmnopqrstuvwxyz'); | ||
kit.et.delete(-1); | ||
expect(kit.peritext.str.view()).toBe('abcdefghklmnopqrstuvwxyz'); | ||
kit.editor.cursor.setAt(5); | ||
kit.et.delete(-1); | ||
expect(kit.peritext.str.view()).toBe('abcdfghklmnopqrstuvwxyz'); | ||
}); | ||
|
||
describe('characters', () => { | ||
describe('can delete all characters backwards', () => { | ||
test('one character at a time', async () => { | ||
const kit = setup(); | ||
const view = kit.peritext.str.view() as string; | ||
const length = view.length; | ||
kit.editor.cursor.setAt(view.length); | ||
expect(kit.peritext.str.view()).toBe(view); | ||
for (let i = length - 1; i >= 0; i--) { | ||
kit.et.delete(-1); | ||
expect(kit.peritext.str.view()).toBe(view.slice(0, i)); | ||
} | ||
expect(kit.peritext.str.view()).toBe(''); | ||
kit.peritext.refresh(); | ||
expect(kit.peritext.str.view()).toBe(''); | ||
}); | ||
|
||
test('two characters at a time', async () => { | ||
const kit = setup(); | ||
const view = kit.peritext.str.view() as string; | ||
const step = 2; | ||
kit.editor.cursor.setAt(view.length); | ||
let i = 1; | ||
expect(kit.peritext.str.view()).toBe(view); | ||
while (kit.editor.text()) { | ||
kit.et.delete(-step); | ||
expect(kit.peritext.str.view()).toBe(view.slice(0, view.length - i * step)); | ||
i++; | ||
} | ||
expect(kit.peritext.str.view()).toBe(''); | ||
}); | ||
|
||
test('five characters at a time', async () => { | ||
const kit = setup(); | ||
const view = kit.peritext.str.view() as string; | ||
const step = 5; | ||
kit.editor.cursor.setAt(view.length); | ||
let i = 1; | ||
expect(kit.peritext.str.view()).toBe(view); | ||
while (kit.editor.text()) { | ||
kit.et.delete(-step); | ||
if (!kit.editor.text()) break; | ||
expect(kit.peritext.str.view()).toBe(view.slice(0, view.length - i * step)); | ||
i++; | ||
} | ||
expect(kit.editor.text()).toBe(''); | ||
}); | ||
}); | ||
|
||
describe('can delete all characters forwards', () => { | ||
test('one character at a time', async () => { | ||
const kit = setup(); | ||
const view = kit.peritext.str.view() as string; | ||
const length = view.length; | ||
kit.editor.cursor.setAt(0); | ||
expect(kit.peritext.str.view()).toBe(view); | ||
for (let i = 1; i <= length; i++) { | ||
kit.et.delete(1); | ||
expect(kit.editor.text()).toBe(view.slice(i)); | ||
} | ||
expect(kit.peritext.str.view()).toBe(''); | ||
kit.peritext.refresh(); | ||
expect(kit.peritext.str.view()).toBe(''); | ||
}); | ||
|
||
test('two characters at a time', async () => { | ||
const kit = setup(); | ||
const view = kit.peritext.str.view() as string; | ||
const step = 2; | ||
kit.editor.cursor.setAt(0); | ||
let i = 1; | ||
expect(kit.peritext.str.view()).toBe(view); | ||
while (kit.editor.text()) { | ||
kit.et.delete(step); | ||
expect(kit.peritext.str.view()).toBe(view.slice(i * step)); | ||
i++; | ||
} | ||
expect(kit.peritext.str.view()).toBe(''); | ||
}); | ||
|
||
test('five characters at a time', async () => { | ||
const kit = setup(); | ||
const view = kit.peritext.str.view() as string; | ||
const step = 5; | ||
kit.editor.cursor.setAt(0); | ||
let i = 1; | ||
expect(kit.peritext.str.view()).toBe(view); | ||
while (kit.editor.text()) { | ||
kit.et.delete(step); | ||
if (!kit.editor.text()) break; | ||
expect(kit.peritext.str.view()).toBe(view.slice(i * step)); | ||
i++; | ||
} | ||
expect(kit.editor.text()).toBe(''); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('words', () => { | ||
test('can delete a word backwards', async () => { | ||
const kit = setup(); | ||
kit.editor.cursor.setAt(10); | ||
kit.et.insert(' '); | ||
kit.editor.cursor.setAt(5); | ||
kit.et.insert(' '); | ||
kit.editor.cursor.setAt(8); | ||
expect(kit.editor.text()).toBe('abcde fghij klmnopqrstuvwxyz'); | ||
kit.et.delete(-1, 'word'); | ||
expect(kit.editor.text()).toBe('abcde hij klmnopqrstuvwxyz'); | ||
}); | ||
|
||
test('can delete a word forwards', async () => { | ||
const kit = setup(); | ||
kit.editor.cursor.setAt(10); | ||
kit.et.insert(' '); | ||
kit.editor.cursor.setAt(5); | ||
kit.et.insert(' '); | ||
kit.editor.cursor.setAt(8); | ||
expect(kit.editor.text()).toBe('abcde fghij klmnopqrstuvwxyz'); | ||
kit.et.delete(1, 'word'); | ||
expect(kit.editor.text()).toBe('abcde fg klmnopqrstuvwxyz'); | ||
}); | ||
|
||
test('can delete a word in both directions', async () => { | ||
const kit = setup(); | ||
kit.editor.cursor.setAt(10); | ||
kit.et.insert(' '); | ||
kit.editor.cursor.setAt(5); | ||
kit.et.insert(' '); | ||
kit.editor.cursor.setAt(8); | ||
expect(kit.editor.text()).toBe('abcde fghij klmnopqrstuvwxyz'); | ||
const DIRECTION = 0; | ||
kit.et.delete(DIRECTION, 'word'); | ||
expect(kit.editor.text()).toBe('abcde klmnopqrstuvwxyz'); | ||
}); | ||
|
||
test('can delete a word at specific position', async () => { | ||
const kit = setup(); | ||
kit.editor.cursor.setAt(10); | ||
kit.et.insert(' '); | ||
kit.editor.cursor.setAt(5); | ||
kit.et.insert(' '); | ||
kit.editor.delCursors(); | ||
expect(kit.editor.text()).toBe('abcde fghij klmnopqrstuvwxyz'); | ||
kit.et.delete(0, 'word', 8); | ||
expect(kit.editor.text()).toBe('abcde klmnopqrstuvwxyz'); | ||
expect(kit.editor.cursor.start.viewPos()).toBe(6); | ||
expect(kit.editor.cursor.isCollapsed()).toBe(true); | ||
kit.et.delete(0, 'word', 3); | ||
expect(kit.editor.text()).toBe(' klmnopqrstuvwxyz'); | ||
expect(kit.editor.cursor.start.viewPos()).toBe(0); | ||
expect(kit.editor.cursor.isCollapsed()).toBe(true); | ||
kit.et.delete(0, 'word', 10); | ||
expect(kit.editor.text()).toBe(' '); | ||
expect(kit.editor.cursor.start.viewPos()).toBe(2); | ||
expect(kit.editor.cursor.isCollapsed()).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('lines', () => { | ||
test('can delete a line backwards', async () => { | ||
const kit = setup(); | ||
kit.editor.cursor.setAt(10); | ||
kit.et.insert('\n'); | ||
kit.editor.cursor.setAt(5); | ||
kit.et.insert('\n'); | ||
kit.editor.cursor.setAt(8); | ||
expect(kit.editor.text()).toBe('abcde\nfghij\nklmnopqrstuvwxyz'); | ||
kit.et.delete(-1, 'line'); | ||
expect(kit.editor.text()).toBe('abcde\nhij\nklmnopqrstuvwxyz'); | ||
}); | ||
|
||
test('can delete a line forwards', async () => { | ||
const kit = setup(); | ||
kit.editor.cursor.setAt(10); | ||
kit.et.insert('\n'); | ||
kit.editor.cursor.setAt(5); | ||
kit.et.insert('\n'); | ||
kit.editor.cursor.setAt(8); | ||
expect(kit.editor.text()).toBe('abcde\nfghij\nklmnopqrstuvwxyz'); | ||
kit.et.delete(1, 'line'); | ||
expect(kit.editor.text()).toBe('abcde\nfg\nklmnopqrstuvwxyz'); | ||
}); | ||
|
||
test('can delete a word in both directions', async () => { | ||
const kit = setup(); | ||
kit.editor.cursor.setAt(10); | ||
kit.et.insert('\n'); | ||
kit.editor.cursor.setAt(5); | ||
kit.et.insert('\n'); | ||
kit.editor.cursor.setAt(8); | ||
expect(kit.editor.text()).toBe('abcde\nfghij\nklmnopqrstuvwxyz'); | ||
const DIRECTION = 0; | ||
kit.et.delete(DIRECTION, 'line'); | ||
expect(kit.editor.text()).toBe('abcde\n\nklmnopqrstuvwxyz'); | ||
}); | ||
}); | ||
}; | ||
|
||
describe('"delete" event', () => { | ||
runAlphabetKitTestSuite(testSuite); | ||
}); |
Oops, something went wrong.