Skip to content

Commit

Permalink
Desktop: By default disable pasting colors in RTE, and added option t…
Browse files Browse the repository at this point in the history
…o enable it
  • Loading branch information
laurent22 committed Nov 9, 2024
1 parent de83b42 commit e16f452
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/app-desktop/gui/NoteEditor/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { FormNote } from './types';

import HtmlToMd from '@joplin/lib/HtmlToMd';
import HtmlToMd, { ParseOptions } from '@joplin/lib/HtmlToMd';
import Note from '@joplin/lib/models/Note';
import { NoteEntity } from '@joplin/lib/services/database/types';
const { MarkupToHtml } = require('@joplin/renderer');

export async function htmlToMarkdown(markupLanguage: number, html: string, originalCss: string): Promise<string> {
export async function htmlToMarkdown(markupLanguage: number, html: string, originalCss: string, parseOptions: ParseOptions = null): Promise<string> {
let newBody = '';

if (markupLanguage === MarkupToHtml.MARKUP_LANGUAGE_MARKDOWN) {
Expand All @@ -14,6 +14,7 @@ export async function htmlToMarkdown(markupLanguage: number, html: string, origi
preserveImageTagsWithSize: true,
preserveNestedTables: true,
preserveColorStyles: true,
...parseOptions,
});
newBody = await Note.replaceResourceExternalToInternalLinks(newBody, { useAbsolutePaths: true });
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export async function processPastedHtml(html: string, htmlToMd: HtmlToMarkdownHa
// TinyMCE, but lost once the note is saved. So here we convert the HTML to Markdown then back
// to HTML to ensure that the content we paste will be handled correctly by the app.
if (htmlToMd && mdToHtml) {
const md = await htmlToMd(MarkupLanguage.Markdown, html, '');
const md = await htmlToMd(MarkupLanguage.Markdown, html, '', { preserveColorStyles: Setting.value('editor.pastePreserveColors') });
html = (await mdToHtml(MarkupLanguage.Markdown, md, markupRenderOptions({ bodyOnly: true }))).html;

// When plugins that add to the end of rendered content are installed, bodyOnly can
Expand Down
3 changes: 2 additions & 1 deletion packages/app-desktop/gui/NoteEditor/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Dispatch } from 'redux';
import { ProcessResultsRow } from '@joplin/lib/services/search/SearchEngine';
import { DropHandler } from './useDropHandler';
import { SearchMarkers } from './useSearchMarkers';
import { ParseOptions } from '@joplin/lib/HtmlToMd';

export interface AllAssetsOptions {
contentMaxWidthTarget?: string;
Expand Down Expand Up @@ -85,7 +86,7 @@ export interface MarkupToHtmlOptions {
}

export type MarkupToHtmlHandler = (markupLanguage: MarkupLanguage, markup: string, options: MarkupToHtmlOptions)=> Promise<RenderResult>;
export type HtmlToMarkdownHandler = (markupLanguage: number, html: string, originalCss: string)=> Promise<string>;
export type HtmlToMarkdownHandler = (markupLanguage: number, html: string, originalCss: string, parseOptions?: ParseOptions)=> Promise<string>;

export interface NoteBodyEditorProps {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
Expand Down
10 changes: 10 additions & 0 deletions packages/lib/models/settings/builtInMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,16 @@ const builtInMetadata = (Setting: typeof SettingType) => {
storage: SettingStorage.File,
isGlobal: true,
},
'editor.pastePreserveColors': {
value: false,
type: SettingItemType.Bool,
public: true,
section: 'note',
appTypes: [AppType.Desktop],
label: () => _('Preserve colours when pasting text in Rich Text Editor'),
storage: SettingStorage.File,
isGlobal: true,
},
'notes.columns': {
value: defaultListColumns(),
public: false,
Expand Down

0 comments on commit e16f452

Please sign in to comment.