-
Notifications
You must be signed in to change notification settings - Fork 1
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 #105 from hw-coconote/fix/HC-210
[FEAT & FIX] 캔버스 동기화 작업, block indent 작업 진행 중
- Loading branch information
Showing
2 changed files
with
180 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import { Extension } from "@tiptap/core"; | ||
// import { Node } from "prosemirror-model"; | ||
import { TextSelection, AllSelection } from "prosemirror-state"; | ||
|
||
export function clamp(val, min, max) { | ||
if (val < min) { | ||
return min; | ||
} | ||
if (val > max) { | ||
return max; | ||
} | ||
return val; | ||
} | ||
|
||
export const IndentProps = { | ||
min: 0, | ||
max: 210, | ||
more: 30, | ||
less: -30, | ||
}; | ||
|
||
export function isBulletListNode(node) { | ||
return node.type.name === "bulletList"; | ||
} | ||
|
||
export function isOrderedListNode(node) { | ||
return node.type.name === "orderedList"; | ||
} | ||
|
||
export function isTodoListNode(node) { | ||
return node.type.name === "listItem"; | ||
} | ||
|
||
export function isListNode(node) { | ||
return isBulletListNode(node) || isOrderedListNode(node) || isTodoListNode(node); | ||
} | ||
|
||
function setNodeIndentMarkup(tr, pos, delta) { | ||
if (!tr.doc) return tr; | ||
|
||
const node = tr.doc.nodeAt(pos); | ||
if (!node) return tr; | ||
|
||
const minIndent = IndentProps.min; | ||
const maxIndent = IndentProps.max; | ||
|
||
const indent = clamp((node.attrs.indent || 0) + delta, minIndent, maxIndent); | ||
|
||
if (indent === node.attrs.indent) return tr; | ||
|
||
const nodeAttrs = { | ||
...node.attrs, | ||
indent, | ||
}; | ||
|
||
return tr.setNodeMarkup(pos, node.type, nodeAttrs, node.marks); | ||
} | ||
|
||
function updateIndentLevel(tr, delta) { | ||
const { doc, selection } = tr; | ||
|
||
if (!doc || !selection) return tr; | ||
|
||
if (!(selection instanceof TextSelection || selection instanceof AllSelection)) { | ||
return tr; | ||
} | ||
|
||
const { from, to } = selection; | ||
|
||
doc.nodesBetween(from, to, (node, pos) => { | ||
const nodeType = node.type; | ||
|
||
if (nodeType.name === "paragraph" || nodeType.name === "heading") { | ||
tr = setNodeIndentMarkup(tr, pos, delta); | ||
return false; | ||
} | ||
if (isListNode(node)) { | ||
return false; | ||
} | ||
return true; | ||
}); | ||
|
||
return tr; | ||
} | ||
|
||
export const Indent = Extension.create({ | ||
name: "indent", | ||
|
||
addOptions() { | ||
return { | ||
types: ["heading", "paragraph"], | ||
indentLevels: [0, 30, 60, 90, 120, 150, 180, 210], | ||
defaultIndentLevel: 0, | ||
}; | ||
}, | ||
|
||
addGlobalAttributes() { | ||
return [ | ||
{ | ||
types: this.options.types, | ||
attributes: { | ||
indent: { | ||
default: this.options.defaultIndentLevel, | ||
renderHTML: (attributes) => { | ||
return { | ||
style: `margin-left: ${attributes.indent}px !important`, | ||
}; | ||
}, | ||
parseHTML: (element) => { | ||
return parseInt(element.style.marginLeft) || this.options.defaultIndentLevel; | ||
}, | ||
}, | ||
}, | ||
}, | ||
]; | ||
}, | ||
|
||
addCommands() { | ||
return { | ||
indent: | ||
() => | ||
({ tr, state, dispatch }) => { | ||
const { selection } = state; | ||
tr = tr.setSelection(selection); | ||
tr = updateIndentLevel(tr, IndentProps.more); | ||
|
||
if (tr.docChanged) { | ||
dispatch && dispatch(tr); | ||
return true; | ||
} | ||
|
||
return false; | ||
}, | ||
outdent: | ||
() => | ||
({ tr, state, dispatch }) => { | ||
const { selection } = state; | ||
tr = tr.setSelection(selection); | ||
tr = updateIndentLevel(tr, IndentProps.less); | ||
|
||
if (tr.docChanged) { | ||
dispatch && dispatch(tr); | ||
return true; | ||
} | ||
|
||
return false; | ||
}, | ||
}; | ||
}, | ||
|
||
addKeyboardShortcuts() { | ||
return { | ||
Tab: () => this.editor.commands.indent(), | ||
"Shift-Tab": () => this.editor.commands.outdent(), | ||
}; | ||
}, | ||
}); |