-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Content Model: Support get and apply segment format (#1518)
- Loading branch information
1 parent
060d3ec
commit 9bd0625
Showing
14 changed files
with
1,032 additions
and
83 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
71 changes: 71 additions & 0 deletions
71
demo/scripts/controls/contentModel/plugins/FormatPainterPlugin.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,71 @@ | ||
import { EditorPlugin, IEditor, PluginEvent, PluginEventType } from 'roosterjs-editor-types'; | ||
import { | ||
applySegmentFormat, | ||
ContentModelSegmentFormat, | ||
getSegmentFormat, | ||
IExperimentalContentModelEditor, | ||
} from 'roosterjs-content-model'; | ||
|
||
const FORMATPAINTERCURSOR_SVG = require('./formatpaintercursor.svg'); | ||
const FORMATPAINTERCURSOR_STYLE = `;cursor: url("${FORMATPAINTERCURSOR_SVG}") 8.5 16, auto`; | ||
const CURSOR_REGEX = /;?\s*cursor:\s*url\(\".*?\"\)[^;]*/gi; | ||
|
||
interface FormatPainterFormatHolder { | ||
format: ContentModelSegmentFormat | null; | ||
} | ||
|
||
export default class FormatPainterPlugin implements EditorPlugin { | ||
private editor: IExperimentalContentModelEditor | null = null; | ||
|
||
getName() { | ||
return 'FormatPainter'; | ||
} | ||
|
||
initialize(editor: IEditor) { | ||
this.editor = editor as IExperimentalContentModelEditor; | ||
} | ||
|
||
dispose() { | ||
this.editor = null; | ||
} | ||
|
||
onPluginEvent(event: PluginEvent) { | ||
if (this.editor && event.eventType == PluginEventType.MouseUp) { | ||
const formatHolder = getFormatHolder(this.editor); | ||
|
||
if (formatHolder.format) { | ||
applySegmentFormat(this.editor, formatHolder.format); | ||
formatHolder.format = null; | ||
|
||
setFormatPainterCursor(this.editor, false /*isOn*/); | ||
} | ||
} | ||
} | ||
|
||
static startFormatPainter(editor: IExperimentalContentModelEditor) { | ||
const formatHolder = getFormatHolder(editor); | ||
const format = getSegmentFormat(editor); | ||
|
||
if (format) { | ||
formatHolder.format = { ...format }; | ||
setFormatPainterCursor(editor, true /*isOn*/); | ||
} | ||
} | ||
} | ||
|
||
function getFormatHolder(editor: IEditor): FormatPainterFormatHolder { | ||
return editor.getCustomData('__FormatPainterFormat', () => { | ||
return {} as FormatPainterFormatHolder; | ||
}); | ||
} | ||
|
||
function setFormatPainterCursor(editor: IEditor, isOn: boolean) { | ||
let styles = editor.getEditorDomAttribute('style') || ''; | ||
styles = styles.replace(CURSOR_REGEX, ''); | ||
|
||
if (isOn) { | ||
styles += FORMATPAINTERCURSOR_STYLE; | ||
} | ||
|
||
editor.setEditorDomAttribute('style', styles); | ||
} |
22 changes: 22 additions & 0 deletions
22
demo/scripts/controls/contentModel/plugins/formatpaintercursor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
19 changes: 19 additions & 0 deletions
19
demo/scripts/controls/ribbonButtons/contentModel/formatPainterButton.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,19 @@ | ||
import FormatPainterPlugin from '../../contentModel/plugins/FormatPainterPlugin'; | ||
import isContentModelEditor from '../../editor/isContentModelEditor'; | ||
import { RibbonButton } from 'roosterjs-react'; | ||
|
||
/** | ||
* @internal | ||
* "Format Painter" button on the format ribbon | ||
*/ | ||
export const formatPainterButton: RibbonButton<'formatPainter'> = { | ||
key: 'formatPainter', | ||
unlocalizedText: 'Format painter', | ||
iconName: 'Brush', | ||
onClick: editor => { | ||
if (isContentModelEditor(editor)) { | ||
FormatPainterPlugin.startFormatPainter(editor); | ||
} | ||
return true; | ||
}, | ||
}; |
80 changes: 80 additions & 0 deletions
80
packages/roosterjs-content-model/lib/domToModel/processors/reducedModelChildProcessor.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,80 @@ | ||
import { contains } from 'roosterjs-editor-dom'; | ||
import { ContentModelBlockGroup } from '../../publicTypes/group/ContentModelBlockGroup'; | ||
import { DomToModelContext } from '../../publicTypes/context/DomToModelContext'; | ||
import { getRegularSelectionOffsets } from '../utils/getRegularSelectionOffsets'; | ||
import { handleRegularSelection, processChildNode } from './childProcessor'; | ||
|
||
interface FormatStateContext extends DomToModelContext { | ||
/** | ||
* An optional stack of parent elements to process. When provided, the child nodes of current parent element will be ignored, | ||
* but use the top element in this stack instead in childProcessor. | ||
*/ | ||
nodeStack?: Node[]; | ||
} | ||
|
||
/** | ||
* @internal | ||
* In order to get format, we can still use the regular child processor. However, to improve performance, we don't need to create | ||
* content model for the whole doc, instead we only need to traverse the tree path that can arrive current selected node. | ||
* This "reduced" child processor will first create a node stack that stores DOM node from root to current common ancestor node of selection, | ||
* then use this stack as a faked DOM tree to create a reduced content model which we can use to retrieve format state | ||
*/ | ||
export function reducedModelChildProcessor( | ||
group: ContentModelBlockGroup, | ||
parent: ParentNode, | ||
context: FormatStateContext | ||
) { | ||
if (context.selectionRootNode) { | ||
if (!context.nodeStack) { | ||
context.nodeStack = createNodeStack(parent, context.selectionRootNode); | ||
} | ||
|
||
const stackChild = context.nodeStack.pop(); | ||
|
||
if (stackChild) { | ||
const [nodeStartOffset, nodeEndOffset] = getRegularSelectionOffsets(context, parent); | ||
|
||
// If selection is not on this node, skip getting node index to save some time since we don't need it here | ||
const index = | ||
nodeStartOffset >= 0 || nodeEndOffset >= 0 ? getChildIndex(parent, stackChild) : -1; | ||
|
||
if (index >= 0) { | ||
handleRegularSelection(index, context, group, nodeStartOffset, nodeEndOffset); | ||
} | ||
|
||
processChildNode(group, stackChild, context); | ||
|
||
if (index >= 0) { | ||
handleRegularSelection(index + 1, context, group, nodeStartOffset, nodeEndOffset); | ||
} | ||
} else { | ||
// No child node from node stack, that means we have reached the deepest node of selection. | ||
// Now we can use default child processor to perform full sub tree scanning for content model, | ||
// So that all selected node will be included. | ||
context.defaultElementProcessors.child(group, parent, context); | ||
} | ||
} | ||
} | ||
|
||
function createNodeStack(root: Node, startNode: Node): Node[] { | ||
const result: Node[] = []; | ||
let node: Node | null = startNode; | ||
|
||
while (node && contains(root, node)) { | ||
result.push(node); | ||
node = node.parentNode; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function getChildIndex(parent: ParentNode, stackChild: Node) { | ||
let index = 0; | ||
let child = parent.firstChild; | ||
|
||
while (child && child != stackChild) { | ||
index++; | ||
child = child.nextSibling; | ||
} | ||
return index; | ||
} |
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
Oops, something went wrong.