-
-
Notifications
You must be signed in to change notification settings - Fork 16
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 #786 from streamich/peritext-fragment-export
Peritext `Fragment` export/import
- Loading branch information
Showing
19 changed files
with
541 additions
and
30 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
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
142 changes: 142 additions & 0 deletions
142
src/json-crdt-extensions/peritext/block/__tests__/Fragment-export.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,142 @@ | ||
import {type Kit, runAlphabetKitTestSuite} from '../../__tests__/setup'; | ||
import {toHtml, toJsonMl} from '../../export/export'; | ||
import {CommonSliceType} from '../../slice'; | ||
|
||
const runTests = (setup: () => Kit) => { | ||
describe('JSON-ML', () => { | ||
test('can export two paragraphs', () => { | ||
const {editor, peritext} = setup(); | ||
editor.cursor.setAt(10); | ||
editor.saved.insMarker(CommonSliceType.p); | ||
peritext.refresh(); | ||
const fragment = peritext.fragment(peritext.rangeAt(4, 10)); | ||
fragment.refresh(); | ||
const html = toJsonMl(fragment.toJson()); | ||
expect(html).toEqual(['', null, ['p', null, 'efghij'], ['p', null, 'klm']]); | ||
}); | ||
|
||
test('can export two paragraphs with inline formatting', () => { | ||
const {editor, peritext} = setup(); | ||
editor.cursor.setAt(10); | ||
editor.saved.insMarker(CommonSliceType.p); | ||
editor.cursor.setAt(6, 2); | ||
editor.saved.insOverwrite(CommonSliceType.b); | ||
editor.cursor.setAt(7, 2); | ||
editor.saved.insOverwrite(CommonSliceType.i); | ||
peritext.refresh(); | ||
const fragment = peritext.fragment(peritext.rangeAt(4, 10)); | ||
fragment.refresh(); | ||
const html = toJsonMl(fragment.toJson()); | ||
expect(html).toEqual([ | ||
'', | ||
null, | ||
['p', null, 'ef', ['b', null, 'g'], ['i', null, ['b', null, 'h']], ['i', null, 'i'], 'j'], | ||
['p', null, 'klm'], | ||
]); | ||
}); | ||
}); | ||
|
||
describe('HTML', () => { | ||
test('can export two paragraphs', () => { | ||
const {editor, peritext} = setup(); | ||
editor.cursor.setAt(10); | ||
editor.saved.insMarker(CommonSliceType.p); | ||
peritext.refresh(); | ||
const fragment = peritext.fragment(peritext.rangeAt(4, 10)); | ||
fragment.refresh(); | ||
const html = toHtml(fragment.toJson()); | ||
expect(html).toBe('<p>efghij</p><p>klm</p>'); | ||
}); | ||
|
||
test('can export two paragraphs (formatted)', () => { | ||
const {editor, peritext} = setup(); | ||
editor.cursor.setAt(10); | ||
editor.saved.insMarker(CommonSliceType.p); | ||
peritext.refresh(); | ||
const fragment = peritext.fragment(peritext.rangeAt(4, 10)); | ||
fragment.refresh(); | ||
const html = toHtml(fragment.toJson(), ' '); | ||
expect(html).toBe('<p>efghij</p>\n<p>klm</p>'); | ||
}); | ||
|
||
test('can export two paragraphs (formatted and wrapped in <div>)', () => { | ||
const {editor, peritext} = setup(); | ||
editor.cursor.setAt(10); | ||
editor.saved.insMarker(CommonSliceType.p); | ||
peritext.refresh(); | ||
const fragment = peritext.fragment(peritext.rangeAt(4, 10)); | ||
fragment.refresh(); | ||
const json = fragment.toJson(); | ||
json[0] = 'div'; | ||
const html = toHtml(json, ' '); | ||
expect(html).toBe('<div>\n <p>efghij</p>\n <p>klm</p>\n</div>'); | ||
}); | ||
|
||
test('can export two paragraphs with inline formatting', () => { | ||
const {editor, peritext} = setup(); | ||
editor.cursor.setAt(10); | ||
editor.saved.insMarker(CommonSliceType.p); | ||
editor.cursor.setAt(6, 2); | ||
editor.saved.insOverwrite(CommonSliceType.b); | ||
editor.cursor.setAt(7, 2); | ||
editor.saved.insOverwrite(CommonSliceType.i); | ||
peritext.refresh(); | ||
const fragment = peritext.fragment(peritext.rangeAt(4, 10)); | ||
fragment.refresh(); | ||
const json = fragment.toJson(); | ||
const html = toHtml(json, ''); | ||
expect(html).toEqual('<p>ef<b>g</b><i><b>h</b></i><i>i</i>j</p><p>klm</p>'); | ||
}); | ||
|
||
test('can export two paragraphs with inline formatting (formatted)', () => { | ||
const {editor, peritext} = setup(); | ||
editor.cursor.setAt(10); | ||
editor.saved.insMarker(CommonSliceType.p); | ||
editor.cursor.setAt(6, 2); | ||
editor.saved.insOverwrite(CommonSliceType.b); | ||
editor.cursor.setAt(7, 2); | ||
editor.saved.insOverwrite(CommonSliceType.i); | ||
peritext.refresh(); | ||
const fragment = peritext.fragment(peritext.rangeAt(4, 10)); | ||
fragment.refresh(); | ||
const json = fragment.toJson(); | ||
const html = toHtml(json, ' '); | ||
expect(html).toEqual( | ||
'<p>\n ef\n <b>g</b>\n <i>\n <b>h</b>\n </i>\n <i>i</i>\n j\n</p>\n<p>klm</p>', | ||
); | ||
}); | ||
|
||
test('can export two paragraphs with inline formatting (formatted, wrapped in <div>)', () => { | ||
const {editor, peritext} = setup(); | ||
editor.cursor.setAt(10); | ||
editor.saved.insMarker(CommonSliceType.p); | ||
editor.cursor.setAt(6, 2); | ||
editor.saved.insOverwrite(CommonSliceType.b); | ||
editor.cursor.setAt(7, 2); | ||
editor.saved.insOverwrite(CommonSliceType.i); | ||
peritext.refresh(); | ||
const fragment = peritext.fragment(peritext.rangeAt(4, 10)); | ||
fragment.refresh(); | ||
const json = fragment.toJson(); | ||
json[0] = 'div'; | ||
const html = toHtml(json, ' '); | ||
expect('\n' + html).toEqual(` | ||
<div> | ||
<p> | ||
ef | ||
<b>g</b> | ||
<i> | ||
<b>h</b> | ||
</i> | ||
<i>i</i> | ||
j | ||
</p> | ||
<p>klm</p> | ||
</div>`); | ||
}); | ||
}); | ||
}; | ||
|
||
describe('Fragment.toJson()', () => { | ||
runAlphabetKitTestSuite(runTests); | ||
}); |
File renamed without changes.
47 changes: 47 additions & 0 deletions
47
src/json-crdt-extensions/peritext/block/__tests__/Fragment-toJson.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,47 @@ | ||
import {type Kit, runAlphabetKitTestSuite} from '../../__tests__/setup'; | ||
import {CommonSliceType} from '../../slice'; | ||
|
||
const runTests = (setup: () => Kit) => { | ||
test('can export two paragraphs', () => { | ||
const {editor, peritext} = setup(); | ||
editor.cursor.setAt(10); | ||
editor.saved.insMarker(CommonSliceType.p); | ||
peritext.refresh(); | ||
const fragment = peritext.fragment(peritext.rangeAt(4, 10)); | ||
fragment.refresh(); | ||
const json = fragment.toJson(); | ||
expect(json).toEqual(['', null, [0, null, 'efghij'], [0, null, 'klm']]); | ||
}); | ||
|
||
test('can export two paragraphs with inline formatting', () => { | ||
const {editor, peritext} = setup(); | ||
editor.cursor.setAt(10); | ||
editor.saved.insMarker(CommonSliceType.p); | ||
editor.cursor.setAt(6, 2); | ||
editor.saved.insOverwrite(CommonSliceType.b); | ||
editor.cursor.setAt(7, 2); | ||
editor.saved.insOverwrite(CommonSliceType.i); | ||
peritext.refresh(); | ||
const fragment = peritext.fragment(peritext.rangeAt(4, 10)); | ||
fragment.refresh(); | ||
const json = fragment.toJson(); | ||
expect(json).toEqual([ | ||
'', | ||
null, | ||
[ | ||
0, | ||
null, | ||
'ef', | ||
[CommonSliceType.b, {inline: true}, 'g'], | ||
[CommonSliceType.i, {inline: true}, [CommonSliceType.b, {inline: true}, 'h']], | ||
[CommonSliceType.i, {inline: true}, 'i'], | ||
'j', | ||
], | ||
[0, null, 'klm'], | ||
]); | ||
}); | ||
}; | ||
|
||
describe('Fragment.toJson()', () => { | ||
runAlphabetKitTestSuite(runTests); | ||
}); |
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,10 @@ | ||
export type PeritextMlNode = string | PeritextMlElement; | ||
export type PeritextMlElement = [ | ||
tag: string | number, | ||
attrs: null | PeritextMlAttributes, | ||
...children: PeritextMlNode[], | ||
]; | ||
export interface PeritextMlAttributes { | ||
inline?: boolean; | ||
data?: unknown; | ||
} |
Oops, something went wrong.