Skip to content
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

Fix for auto style restore to default on press enter and support for inline attachment #139

Merged
merged 5 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions src/store/integrations/composer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import React, { FC, useCallback, useMemo } from 'react';
import React, { FC, useCallback, useMemo, useRef } from 'react';
import { Container } from '@zextras/carbonio-design-system';
import styled from 'styled-components';
// TinyMCE so the global var exists
// eslint-disable-next-line no-unused-vars
import tinymce from 'tinymce/tinymce';
Expand Down Expand Up @@ -44,6 +45,7 @@ import 'tinymce/plugins/directionality';
import 'tinymce/plugins/autoresize';

import { Editor } from '@tinymce/tinymce-react';
import { useTranslation } from 'react-i18next';
import { useUserSettings } from '../account';

type ComposerProps = {
Expand All @@ -57,14 +59,21 @@ type ComposerProps = {
value?: string;
/** The base url to append to the resource urls */
baseAssetsUrl?: string;
onFileSelect?: (arg: any) => void;
};

export const FileInput = styled.input`
display: none;
`;

const Composer: FC<ComposerProps> = ({
onEditorChange,
onFileSelect,
inline = false,
value,
baseAssetsUrl,
initialValue,

...rest
}) => {
const _onEditorChange = useCallback(
Expand All @@ -85,20 +94,58 @@ const Composer: FC<ComposerProps> = ({
}),
[prefs]
);

const inputRef = useRef<any>();
const onFileClick = useCallback(() => {
if (inputRef.current) {
inputRef.current.value = null;
inputRef.current.click();
}
}, []);
const { t } = useTranslation();
const inlineLabel = useMemo(() => t('label.add_inline_image', 'Add inline image'), [t]);
return (
<Container
height="100%"
crossAlignment="baseline"
mainAlignment="flex-start"
style={{ overflowY: 'hidden' }}
>
<FileInput
type="file"
ref={inputRef}
accept="image/*"
onChange={(): void => {
onFileSelect && onFileSelect({ editor: tinymce, files: inputRef?.current?.files });
}}
multiple
/>

<Editor
initialValue={initialValue}
value={value}
init={{
content_css: `${baseAssetsUrl}/tinymce/skins/content/default/content.css`,
setup: (editor: any): void => {
if (onFileSelect)
editor.ui.registry.addMenuButton('imageSelector', {
icon: 'gallery',
tooltip: 'Select Image',
fetch: (callback: any) => {
const items = [
{
type: 'menuitem',
text: inlineLabel,
onAction: (): void => {
onFileClick();
}
}
];
callback(items);
}
});
},
min_height: 350,
auto_focus: true,
menubar: false,
statusbar: false,
branding: false,
Expand Down Expand Up @@ -170,15 +217,15 @@ const Composer: FC<ComposerProps> = ({
toolbar: inline
? false
: // eslint-disable-next-line max-len
'fontselect fontsizeselect styleselect visualblocks| bold italic underline strikethrough | removeformat code | alignleft aligncenter alignright alignjustify | forecolor backcolor | bullist numlist outdent indent | ltr rtl | insertfile image ',
'fontselect fontsizeselect styleselect visualblocks| bold italic underline strikethrough | removeformat code | alignleft aligncenter alignright alignjustify | forecolor backcolor | bullist numlist outdent indent | ltr rtl | insertfile image | imageSelector ',
quickbars_insert_toolbar: inline ? 'bullist numlist' : '',
quickbars_selection_toolbar: inline
? 'bold italic underline | forecolor backcolor | removeformat | quicklink'
: 'quicklink',
contextmenu: inline ? '' : '',
toolbar_mode: 'wrap',
forced_root_block: false,
content_style: `body { color: ${defaultStyle?.color}; font-size: ${defaultStyle?.fontSize}; font-family: ${defaultStyle?.font}; }`,
content_style: `body { color: ${defaultStyle?.color}; font-size: ${defaultStyle?.fontSize}; font-family: ${defaultStyle?.font}; }`,
visualblocks_default_state: false,
end_container_on_empty_block: true
}}
Expand Down
2 changes: 1 addition & 1 deletion translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"accounts_list": "Accounts list",
"add_delegate": "Add delegate",
"add_external_account": "Add external account",
"add_inline_image": "Add inline image",
"add_new_email": "Add new e-mail address",
"add_persona": "Add persona",
"add_recovery_email": "Add recovery e-mail",
Expand Down Expand Up @@ -60,7 +61,6 @@
"feedback": "Feedback",
"filter_folders": "Filter folders",
"folder": "Folder: New External Account 3",
"folder_external_account": "Folder: New External Account",
"folders": "Folders",
"from_name": "From: \"Name\"",
"go_back": "Go Back",
Expand Down