-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add FoldingHeading extension (#314)
* fix(YfmHeading): improve folding behaviour * feat: add FoldingHeading extension
- Loading branch information
Showing
30 changed files
with
720 additions
and
17 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type {Action, ExtensionAuto} from '../../../core'; | ||
|
||
import {FoldingHeadingSpecs} from './FoldingHeadingSpec'; | ||
import {toggleHeadingFoldingAction} from './actions'; | ||
import { | ||
openHeadingAndCreateParagraphAfterIfCursorAtEndOfHeading, | ||
removeFoldingIfCursorAtStartOfHeading, | ||
} from './commands'; | ||
import {foldingHeadingRule} from './input-rules'; | ||
import {foldingPlugin} from './plugins/Folding'; | ||
import {headingType} from './utils'; | ||
|
||
import '@diplodoc/folding-headings-extension/runtime/styles.css'; | ||
|
||
const action = 'toggleHeadingFolding'; | ||
|
||
export const FoldingHeading: ExtensionAuto = (builder) => { | ||
builder.use(FoldingHeadingSpecs); | ||
|
||
builder.addAction(action, () => toggleHeadingFoldingAction); | ||
builder.addInputRules(({schema}) => ({rules: [foldingHeadingRule(headingType(schema), 6)]})); | ||
builder.addKeymap( | ||
() => ({ | ||
Enter: openHeadingAndCreateParagraphAfterIfCursorAtEndOfHeading, | ||
Backspace: removeFoldingIfCursorAtStartOfHeading, | ||
}), | ||
builder.Priority.High, | ||
); | ||
builder.addPlugin(foldingPlugin); | ||
}; | ||
|
||
declare global { | ||
namespace WysiwygEditor { | ||
interface Actions { | ||
[action]: Action; | ||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/extensions/yfm/FoldingHeading/FoldingHeadingSpec/FoldingHeadingSpecs.test.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,100 @@ | ||
import {builders} from 'prosemirror-test-builder'; | ||
|
||
import {createMarkupChecker} from '../../../../../tests/sameMarkup'; | ||
import {ExtensionsManager} from '../../../../core'; | ||
import {BaseNode, BaseSchemaSpecs} from '../../../base/specs'; | ||
import {ItalicSpecs, headingNodeName, italicMarkName} from '../../../markdown/specs'; | ||
import {YfmHeadingAttr, YfmHeadingSpecs} from '../../../yfm/specs'; | ||
|
||
import {FoldingHeadingSpecs} from './FoldingHeadingSpecs'; | ||
|
||
const {schema, markupParser, serializer} = new ExtensionsManager({ | ||
extensions: (builder) => | ||
builder | ||
.use(BaseSchemaSpecs, {}) | ||
.use(ItalicSpecs) | ||
.use(YfmHeadingSpecs, {}) | ||
.use(FoldingHeadingSpecs), | ||
}).buildDeps(); | ||
|
||
const {doc, h1, h2, h3, h4, h5, h6, i} = builders< | ||
'doc' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6', | ||
'i' | ||
>(schema, { | ||
doc: {nodeType: BaseNode.Doc}, | ||
h1: {nodeType: headingNodeName, [YfmHeadingAttr.Level]: 1}, | ||
h2: {nodeType: headingNodeName, [YfmHeadingAttr.Level]: 2}, | ||
h3: {nodeType: headingNodeName, [YfmHeadingAttr.Level]: 3}, | ||
h4: {nodeType: headingNodeName, [YfmHeadingAttr.Level]: 4}, | ||
h5: {nodeType: headingNodeName, [YfmHeadingAttr.Level]: 5}, | ||
h6: {nodeType: headingNodeName, [YfmHeadingAttr.Level]: 6}, | ||
i: {markType: italicMarkName}, | ||
}); | ||
|
||
const {same} = createMarkupChecker({parser: markupParser, serializer}); | ||
|
||
describe('Folding Headings', () => { | ||
it('should parse folding headings', () => { | ||
const markup = ` | ||
#+ heading 1 | ||
##+ heading 2 | ||
###+ heading 3 | ||
####+ heading 4 | ||
#####+ heading 5 | ||
######+ heading 6 | ||
`.trim(); | ||
|
||
return same( | ||
markup, | ||
doc( | ||
h1({[YfmHeadingAttr.Folding]: true}, 'heading 1'), | ||
h2({[YfmHeadingAttr.Folding]: true}, 'heading 2'), | ||
h3({[YfmHeadingAttr.Folding]: true}, 'heading 3'), | ||
h4({[YfmHeadingAttr.Folding]: true}, 'heading 4'), | ||
h5({[YfmHeadingAttr.Folding]: true}, 'heading 5'), | ||
h6({[YfmHeadingAttr.Folding]: true}, 'heading 6'), | ||
), | ||
); | ||
}); | ||
|
||
it('should parse common headings', () => { | ||
const markup = ` | ||
# heading 1 | ||
## heading 2 | ||
### heading 3 | ||
#### heading 4 | ||
##### heading 5 | ||
###### heading 6 | ||
`.trim(); | ||
|
||
return same( | ||
markup, | ||
doc( | ||
h1('heading 1'), | ||
h2('heading 2'), | ||
h3('heading 3'), | ||
h4('heading 4'), | ||
h5('heading 5'), | ||
h6('heading 6'), | ||
), | ||
); | ||
}); | ||
|
||
it('should parse folding heading with inline markup', () => { | ||
const markup = ` | ||
##+ *heading* 2 | ||
`.trim(); | ||
|
||
return same(markup, doc(h2({[YfmHeadingAttr.Folding]: true}, i('heading'), ' 2'))); | ||
}); | ||
}); |
Oops, something went wrong.