-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for adding/removing space before/after paragraphs in content model #1565
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6fb825e
Create api for block margins
ianeli1 fa361b6
Create space before after buttons
ianeli1 3bc095d
Update testing
ianeli1 20455cf
Fix by using decorator
ianeli1 f65d0cc
Replace positive value with none
ianeli1 b112452
Treat no margin as space removed
ianeli1 ac805bd
Fix testing
ianeli1 629713e
Merge branch 'master' into u/ianeli/spacing-margins
ianeli1 0b84490
Improve tag name selecting
ianeli1 f5f4e3d
testing
ianeli1 b4a8e47
Merge branch 'master' into u/ianeli/spacing-margins
ianeli1 af7ba98
rename and improve logic
ianeli1 3d6b83d
Add support for formatState on onClick call
ianeli1 edce3ad
Update button impl
ianeli1 d2d3896
Revert "Update button impl"
ianeli1 06919f9
Revert "Add support for formatState on onClick call"
ianeli1 7a2b2ce
use getFormatState()
ianeli1 64ad43d
Update test name
ianeli1 cb35b55
Merge branch 'master' into u/ianeli/spacing-margins
ianeli1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
49 changes: 49 additions & 0 deletions
49
demo/scripts/controls/ribbonButtons/contentModel/spaceBeforeAfterButtons.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,49 @@ | ||
import { RibbonButton } from 'roosterjs-react'; | ||
import { getFormatState, isContentModelEditor, setParagraphMargin } from 'roosterjs-content-model'; | ||
|
||
const spaceAfterButtonKey = 'buttonNameSpaceAfter'; | ||
const spaceBeforeButtonKey = 'buttonNameSpaceBefore'; | ||
|
||
/** | ||
* @internal | ||
* "Add space after" button on the format ribbon | ||
*/ | ||
export const spaceAfterButton: RibbonButton<typeof spaceAfterButtonKey> = { | ||
key: spaceAfterButtonKey, | ||
unlocalizedText: 'Remove space after', | ||
iconName: 'CaretDown8', | ||
isChecked: formatState => !formatState.marginBottom || parseInt(formatState.marginBottom) <= 0, | ||
onClick: editor => { | ||
if (isContentModelEditor(editor)) { | ||
const marginBottom = getFormatState(editor).marginBottom; | ||
setParagraphMargin( | ||
editor, | ||
undefined /* marginTop */, | ||
parseInt(marginBottom) ? null : '8pt' | ||
); | ||
} | ||
return true; | ||
}, | ||
}; | ||
|
||
/** | ||
* @internal | ||
* "Add space before" button on the format ribbon | ||
*/ | ||
export const spaceBeforeButton: RibbonButton<typeof spaceBeforeButtonKey> = { | ||
key: spaceBeforeButtonKey, | ||
unlocalizedText: 'Add space before', | ||
iconName: 'CaretUp8', | ||
isChecked: formatState => parseInt(formatState.marginTop) > 0, | ||
onClick: editor => { | ||
if (isContentModelEditor(editor)) { | ||
const marginTop = getFormatState(editor).marginTop; | ||
setParagraphMargin( | ||
editor, | ||
parseInt(marginTop) ? null : '12pt', | ||
undefined /* marginBottom */ | ||
); | ||
} | ||
return true; | ||
}, | ||
}; |
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
34 changes: 34 additions & 0 deletions
34
packages/roosterjs-content-model/lib/publicApi/block/setParagraphMargin.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,34 @@ | ||
import { createParagraphDecorator } from '../../modelApi/creators/createParagraphDecorator'; | ||
import { IContentModelEditor } from '../../publicTypes/IContentModelEditor'; | ||
import { formatParagraphWithContentModel } from '../utils/formatParagraphWithContentModel'; | ||
|
||
/** | ||
* Toggles the current block(s) margin properties. | ||
* null deletes any existing value, undefined is ignored | ||
* @param editor The editor to operate on | ||
* @param marginTop value for top margin | ||
* @param marginBottom value for bottom margin | ||
*/ | ||
export default function setParagraphMargin( | ||
editor: IContentModelEditor, | ||
marginTop?: string | null, | ||
marginBottom?: string | null | ||
) { | ||
formatParagraphWithContentModel(editor, 'setParagraphMargin', para => { | ||
if (!para.decorator) { | ||
para.decorator = createParagraphDecorator('p'); | ||
} | ||
|
||
if (marginTop) { | ||
para.format.marginTop = marginTop; | ||
} else if (marginTop === null) { | ||
delete para.format.marginTop; | ||
} | ||
|
||
if (marginBottom) { | ||
para.format.marginBottom = marginBottom; | ||
} else if (marginBottom === null) { | ||
delete para.format.marginBottom; | ||
} | ||
}); | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want to do a toggle, you can get format state using getFormatState() here. Do not let plugin pass the format to you. Or if you really want, make it an optional parameter so that we know it can be null.