-
Notifications
You must be signed in to change notification settings - Fork 37
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 #1624 from SenseNet/feature/Tinymce-editor
Feature Tinymce Editor
- Loading branch information
Showing
23 changed files
with
2,788 additions
and
4,118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,3 +68,5 @@ cypress/screenshots | |
|
||
# Instrumented code for cypress code coverage | ||
instrumented | ||
|
||
/tinymce |
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
15 changes: 15 additions & 0 deletions
15
apps/sensenet/src/components/field-controls/tinymce-editor.tsx
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,15 @@ | ||
/** | ||
* @module FieldControls | ||
*/ | ||
import { ReactClientFieldSetting, TinymceEditor as SnTinymceEditor } from '@sensenet/controls-react' | ||
import React from 'react' | ||
import { useLocalization } from '../../hooks' | ||
|
||
/** | ||
* Field control that represents a RichText field. Available values will be populated from the FieldSettings. | ||
*/ | ||
export const TinymceEditor: React.FC<ReactClientFieldSetting> = (props) => { | ||
const localization = useLocalization() | ||
|
||
return <SnTinymceEditor {...props} localization={{ richTextEditor: localization.editor }} /> | ||
} |
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 |
---|---|---|
@@ -1 +1,11 @@ | ||
window.matchMedia = | ||
window.matchMedia || | ||
function () { | ||
return { | ||
matches: false, | ||
addListener() {}, | ||
removeListener() {}, | ||
} | ||
} | ||
|
||
require('isomorphic-fetch') |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export { Editor as default } from '@sensenet/editor-react' | ||
export { Editor as default, TinymceEditor } from '@sensenet/editor-react' |
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
108 changes: 108 additions & 0 deletions
108
packages/sn-controls-react/src/fieldcontrols/tinymce-editor.tsx
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,108 @@ | ||
/** | ||
* @module FieldControls | ||
*/ | ||
import { CircularProgress, createStyles, FormHelperText, InputLabel, makeStyles, Typography } from '@material-ui/core' | ||
import { deepMerge } from '@sensenet/client-utils' | ||
import { renderHtml } from '@sensenet/editor-react' | ||
import React, { lazy, Suspense } from 'react' | ||
import { changeTemplatedValue } from '../helpers' | ||
import { ReactClientFieldSetting } from './client-field-setting' | ||
import { defaultLocalization } from './localization' | ||
const Editor = lazy(() => import('../editor-wrapper').then((module) => ({ default: module.TinymceEditor }))) | ||
|
||
const useStyles = makeStyles(() => | ||
createStyles({ | ||
richTextEditor: {}, | ||
}), | ||
) | ||
|
||
type RichTextEditorClassKey = Partial<ReturnType<typeof useStyles>> | ||
|
||
interface ParsedRichTextFieldValue { | ||
text: string | ||
editor: string | ||
} | ||
|
||
const getFieldValue = (rawValue?: string) => { | ||
let value | ||
|
||
if (rawValue === undefined || rawValue === null) { | ||
return undefined | ||
} | ||
|
||
try { | ||
value = JSON.parse(rawValue) as ParsedRichTextFieldValue | ||
} catch (_) { | ||
return rawValue | ||
} | ||
|
||
try { | ||
return value.editor ? JSON.parse(value.editor) : value.text | ||
} catch (_) { | ||
return value.text | ||
} | ||
} | ||
|
||
/** | ||
* Field control that represents a LongText field. Available values will be populated from the FieldSettings. | ||
*/ | ||
export const TinymceEditor: React.FC< | ||
ReactClientFieldSetting & { classes?: RichTextEditorClassKey; fieldValue?: string } | ||
> = (props) => { | ||
const localization = deepMerge(defaultLocalization.richTextEditor, props.localization?.richTextEditor) | ||
|
||
const initialState = | ||
getFieldValue(props.fieldValue) || | ||
(props.actionName === 'new' && changeTemplatedValue(props.settings.DefaultValue)) || | ||
'' | ||
const classes = useStyles(props) | ||
|
||
switch (props.actionName) { | ||
case 'edit': | ||
case 'new': | ||
return ( | ||
<div className={classes.richTextEditor}> | ||
<InputLabel shrink htmlFor={props.settings.Name} required={props.settings.Compulsory}> | ||
{props.settings.DisplayName} | ||
</InputLabel> | ||
|
||
<Suspense | ||
fallback={ | ||
<div style={{ textAlign: 'center' }}> | ||
<CircularProgress /> | ||
<div>{localization.loading}</div> | ||
</div> | ||
}> | ||
<Editor | ||
initvalue={initialState} | ||
onChange={(content) => { | ||
props.fieldOnChange?.(props.settings.Name, content) | ||
}} | ||
/> | ||
</Suspense> | ||
|
||
{!props.hideDescription && <FormHelperText>{props.settings.Description}</FormHelperText>} | ||
</div> | ||
) | ||
case 'browse': | ||
default: | ||
return ( | ||
<div> | ||
<Typography variant="caption" gutterBottom={true}> | ||
{props.settings.DisplayName} | ||
</Typography> | ||
{initialState ? ( | ||
<div | ||
dangerouslySetInnerHTML={{ | ||
__html: typeof initialState === 'string' ? initialState : renderHtml(initialState), | ||
}} | ||
/> | ||
) : ( | ||
<Typography variant="body1" gutterBottom={true}> | ||
{localization.noValue} | ||
</Typography> | ||
)} | ||
</div> | ||
) | ||
} | ||
} |
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 |
---|---|---|
|
@@ -60,3 +60,4 @@ typings/ | |
dist | ||
temp | ||
bundle | ||
/public/tinymce/ |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './components' | ||
export * from './context' | ||
export * from './utils' | ||
export * from './tinymce' |
Oops, something went wrong.