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
Inline text formatting #1323
Merged
Merged
Inline text formatting #1323
Changes from 3 commits
Commits
Show all changes
63 commits
Select commit
Hold shift + click to select a range
536ac78
WIP: First partially working version of inline text formatting
2e023e8
Merge branch 'master' into feature/inline-rich-text
854510f
Added fix for importing elements with multiple styles
6fdac4c
Added the option to toggle b/i/u for entire text field
e0506bf
Cleaned up the structure a bit
323ec6c
Added support for font weight and multi-select
6377f5a
Update tests now that some values are not in use
48c765d
Adding `npx` to postinstall to see if CI approves
b0ba301
Removed `postinstall-postinstall`
1656c0a
If selection is collapsed when applying style change, apply to inline…
ad34f87
Reorganised selection manip and added letter spacing
4a28da7
Disable force focus when setting letter spacing
6346fa2
Added inline text color formatting
ebf8f26
Fix multiple reducer for patterns, and fix tests
04af4f5
Fixed font and font weight dropdowns
e17c4f6
Remove deprecated types from text element
47df628
Some extra cleanup of deprecated properties
5dcaf4e
Added new migration with test (also added `jest-extended`)
0f49ebd
Merge branch 'master' into feature/inline-rich-text
e8b1f4e
Merge branch 'master' into feature/inline-rich-text
9739dfb
Refactored formatters to verticals rather than horizontals - much cle…
41e6954
Fixed reducer and added comments
97a91cf
Added jsdocs to html manipulation
c4f4c18
Removed classes from migration and added test for nested tags
057f33c
Fix tests for text style now that export does not have classes anymore
8097c3f
Moved focus management to edit component
8685d11
Create general `getValidHTML` util
3a80904
Added deps to `useImperativeHandle`
e2d2f23
Fixed `parseFloat` args
4f58366
Merge branch 'master' into feature/inline-rich-text
a0b3259
Added faux-selection style and fixed various bugs
59134bc
Fixed typo
ab85df5
For now, strip formatting when pasting
37c9433
Merge branch 'master' into feature/inline-rich-text
2f09bc6
Remove weird npm scripts
7e2ed36
Using a less nice matcher
102c5af
Added tests for formatters
4f88010
Merge branch 'master' into feature/inline-rich-text
aa2fff6
Merge branch 'master' into feature/inline-rich-text
44d7a77
Set default font color to black - all other colors are fixed inline
b5a0c76
Added unit tests for style manipulation
080251b
Merge branch 'master' into feature/inline-rich-text
85edb58
Fixed color input (allow multiple value string)
e8f6266
Fixed color picker to default to solid black (rather than transparent…
f1dcb73
Fixed null value for input
03aa64b
Fix color preset to work for new ITF formatting
1b4f9de
Added full support for text color and text style presets
f0dd127
Inlined font weights for text panel presets
d411b08
Fix focus out to check click target in capture phase
f1c8468
Merge branch 'master' into feature/inline-rich-text
16dbf27
Re-add default props to test
e295b37
Added jsdoc for draftUtils
870f2bd
Fix children proptype
e03baf3
Add better error handlign to faux selection removal
0081a79
Added jsdocs for complex style manipulation functions
c5adfec
Added function to strip relevant inline styling
cd77cb9
Merge branch 'master' into feature/inline-rich-text
7a28375
Merge branch 'master' into feature/inline-rich-text
ba711d7
Added new function to compare patterns and used where applicable
b63842a
Added ignore black to set color style as it is default
b6468b0
Changed highlight to use existing style manipulation
595d4dd
Merge branch 'master' into feature/inline-rich-text
13b6fda
Removed conditional hook
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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,24 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { createContext } from 'react'; | ||
|
||
const RichTextContext = createContext({ state: {}, actions: {} }); | ||
|
||
export default RichTextContext; |
45 changes: 45 additions & 0 deletions
45
assets/src/edit-story/components/richText/customConstants.js
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,45 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export const NONE = 'NONE'; | ||
export const ITALIC = 'CUSTOM-ITALIC'; | ||
export const UNDERLINE = 'CUSTOM-UNDERLINE'; | ||
export const WEIGHT = 'CUSTOM-WEIGHT'; | ||
export const COLOR = 'CUSTOM-COLOR'; | ||
export const LETTERSPACING = 'CUSTOM-LETTERSPACING'; | ||
|
||
export const SMALLEST_BOLD = 600; | ||
export const DEFAULT_BOLD = 700; | ||
|
||
export const weightToStyle = (weight) => `${WEIGHT}-${weight}`; | ||
export const colorToStyle = (color) => `${COLOR}-${color}`; | ||
export const lsToStyle = (ls) => `${LETTERSPACING}-${ls}`; | ||
|
||
export const isStyle = (style, prefix) => | ||
typeof style === 'string' && style.startsWith(prefix); | ||
export const getVariable = (style, prefix) => style.slice(prefix.length + 1); | ||
|
||
export const styleToWeight = (style) => | ||
isStyle(style, WEIGHT) ? parseInt(getVariable(style, WEIGHT)) : null; | ||
export const styleToColor = (style) => | ||
isStyle(style, COLOR) ? getVariable(style, COLOR) : null; | ||
export const styleToLs = (style) => | ||
isStyle(style, LETTERSPACING) | ||
? parseInt(getVariable(style, LETTERSPACING)) | ||
: null; | ||
|
||
export const isBold = (style) => styleToWeight(style) >= 700; | ||
export const hasBold = (styles) => styles.some(isBold); |
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,81 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { stateToHTML } from 'draft-js-export-html'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { styleToWeight, ITALIC, UNDERLINE } from './customConstants'; | ||
|
||
function inlineStyleFn(styles) { | ||
const inline = styles.toArray().reduce( | ||
({ classes, css }, style) => { | ||
// Italic | ||
if (style === ITALIC) { | ||
return { | ||
classes: [...classes, 'italic'], | ||
barklund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
css: { ...css, fontStyle: 'italic' }, | ||
}; | ||
} | ||
|
||
// Underline | ||
if (style === UNDERLINE) { | ||
return { | ||
classes: [...classes, 'underline'], | ||
css: { ...css, textDecoration: 'underline' }, | ||
}; | ||
} | ||
|
||
// Weight | ||
const weight = styleToWeight(style); | ||
if (weight) { | ||
return { | ||
classes: [...classes, 'weight'], | ||
css: { ...css, fontWeight: weight }, | ||
}; | ||
} | ||
|
||
// TODO: Color | ||
// TODO: Letter spacing | ||
|
||
return { classes, css }; | ||
}, | ||
{ classes: [], css: {} } | ||
); | ||
|
||
if (inline.classes.length === 0) { | ||
return null; | ||
} | ||
|
||
return { | ||
element: 'span', | ||
attributes: { class: inline.classes.join(' ') }, | ||
style: inline.css, | ||
}; | ||
} | ||
|
||
function exportHTML(editorState) { | ||
return stateToHTML(editorState.getCurrentContent(), { | ||
inlineStyleFn, | ||
defaultBlockTag: null, | ||
}); | ||
} | ||
|
||
export default exportHTML; |
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,78 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { stateFromHTML } from 'draft-js-import-html'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { weightToStyle, ITALIC, UNDERLINE } from './customConstants'; | ||
import { draftMarkupToContent } from './util'; | ||
|
||
function customInlineFn(element, { Style }) { | ||
switch (element.tagName.toLowerCase()) { | ||
case 'span': { | ||
const styles = [...element.classList] | ||
.map((className) => { | ||
switch (className) { | ||
case 'weight': { | ||
const fontWeight = parseInt(element.style.fontWeight) || 400; | ||
return weightToStyle(fontWeight); | ||
} | ||
|
||
case 'italic': | ||
return ITALIC; | ||
|
||
case 'underline': | ||
return UNDERLINE; | ||
|
||
default: | ||
return null; | ||
} | ||
}) | ||
.filter((style) => Boolean(style)); | ||
barklund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (styles.length) { | ||
// This is the reason we need a patch, as multiple styles aren't supported by published package | ||
// and maintainer clearly doesn't care about it enough to merge. | ||
// see: <rootDir>/patches/draft-js-import-element+1.4.0.patch | ||
return Style(styles); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
default: | ||
return null; | ||
} | ||
} | ||
|
||
function importHTML(html) { | ||
const htmlWithBreaks = (html || '') | ||
// Re-insert manual line-breaks for empty lines | ||
.replace(/\n(?=\n)/g, '\n<br />') | ||
.split('\n') | ||
.map((s) => { | ||
return `<p>${draftMarkupToContent(s)}</p>`; | ||
}) | ||
.join(''); | ||
return stateFromHTML(htmlWithBreaks, { customInlineFn }); | ||
} | ||
|
||
export default importHTML; |
47 changes: 47 additions & 0 deletions
47
assets/src/edit-story/components/richText/customInlineDisplay.js
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,47 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { styleToWeight, ITALIC, UNDERLINE } from './customConstants'; | ||
|
||
function customInlineDisplay(styles) { | ||
return styles.toArray().reduce((css, style) => { | ||
// Italic | ||
if (style === ITALIC) { | ||
barklund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return { ...css, fontStyle: 'italic' }; | ||
} | ||
|
||
// Underline | ||
if (style === UNDERLINE) { | ||
return { ...css, textDecoration: 'underline' }; | ||
} | ||
|
||
// Weight | ||
const weight = styleToWeight(style); | ||
if (weight) { | ||
return { ...css, fontWeight: weight }; | ||
} | ||
|
||
// TODO: Color | ||
// TODO: Letter spacing | ||
|
||
return css; | ||
}, {}); | ||
} | ||
|
||
export default customInlineDisplay; |
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.
Just out of curiosity, would it have worked if we simply used here CSS notation. E.g.
BOLD-700
becomes a custom stylefont-weight:700
(orfontWeight:700
) instead? And so on. If possible, this could possibly simplify some of the downstream code.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.
Good idea. I'll think about it as I'll be refactoring this part of the codebase.
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.
I have not changed this part as of yet. It's still an open option though.