-
-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for indent/outdent
- Loading branch information
Showing
14 changed files
with
182 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import type { EditorState, Transaction, Command } from 'prosemirror-state'; | ||
|
||
import { clamp } from 'ngx-editor/utils'; | ||
import { InsertCommand } from './types'; | ||
|
||
const indentNodeTypes = ['paragraph', 'heading', 'blockquote']; | ||
|
||
type IndentMethod = 'increase' | 'decrease'; | ||
const minIndent = 0; | ||
const maxIndent = 10; | ||
|
||
const udpateIndentLevel = (tr: Transaction, pos: number, method: IndentMethod): boolean => { | ||
const node = tr.doc.nodeAt(pos); | ||
if (!node) { return false; } | ||
|
||
const nodeIndent = node.attrs['indent'] ?? 0; | ||
const newIndent = clamp(nodeIndent + (method === 'increase' ? 1 : -1), minIndent, maxIndent); | ||
|
||
if (newIndent === nodeIndent || newIndent < minIndent || newIndent > maxIndent) { | ||
return false; | ||
} | ||
|
||
const attrs = { | ||
...node.attrs, | ||
indent: newIndent, | ||
}; | ||
|
||
tr.setNodeMarkup(pos, node.type, attrs); | ||
return true; | ||
}; | ||
|
||
class Indent implements InsertCommand { | ||
method: IndentMethod = 'increase'; | ||
|
||
constructor(method: IndentMethod) { | ||
this.method = method; | ||
} | ||
|
||
insert(): Command { | ||
return (state: EditorState, dispatch?: (tr: Transaction) => void): boolean => { | ||
const { tr, doc } = state; | ||
const { from, to } = tr.selection; | ||
|
||
let applicable = false; | ||
|
||
doc.nodesBetween(from, to, (node, pos) => { | ||
const nodeType = node.type; | ||
|
||
if (indentNodeTypes.includes(nodeType.name)) { | ||
applicable = udpateIndentLevel(tr, pos, this.method); | ||
return false; | ||
} else if (node.type.name.includes('list')) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
if (!applicable) { | ||
return false; | ||
} | ||
|
||
if (tr.docChanged) { | ||
dispatch?.(tr); | ||
} | ||
|
||
return true; | ||
}; | ||
} | ||
|
||
canExecute(state: EditorState): boolean { | ||
return this.insert()(state); | ||
} | ||
} | ||
|
||
export default Indent; |
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 @@ | ||
export default '<path d="M0 0h24v24H0V0z" fill="none"/><path d="M3 21h18v-2H3v2zM3 8v8l4-4-4-4zm8 9h10v-2H11v2zM3 3v2h18V3H3zm8 6h10V7H11v2zm0 4h10v-2H11v2z"/>'; |
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 @@ | ||
export default '<path d="M0 0h24v24H0V0z" fill="none"/><path d="M11 17h10v-2H11v2zm-8-5l4 4V8l-4 4zm0 9h18v-2H3v2zM3 3v2h18V3H3zm8 6h10V7H11v2zm0 4h10v-2H11v2z"/>'; |
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,5 @@ | ||
const clamp = (value: number, min: number, max: number): number => { | ||
return Math.min(Math.max(value, min), max); | ||
}; | ||
|
||
export default clamp; |
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