From adea7f535d5a1411b3260bd67c54628bdb2d2b7c Mon Sep 17 00:00:00 2001 From: Renato Atilio Date: Mon, 4 Nov 2024 14:13:43 -0300 Subject: [PATCH] DEV: prosemirror --- .../custom-typographer-replacements.js | 50 +- .../app/components/composer-container.hbs | 22 +- .../app/components/composer-editor.js | 22 +- .../app/components/composer/toggle-switch.gjs | 35 ++ .../discourse/app/components/d-editor.hbs | 11 +- .../discourse/app/components/d-editor.js | 59 ++- .../lib/composer/rich-editor-extensions.js | 208 ++++++++ .../discourse/app/lib/plugin-api.gjs | 7 +- .../javascripts/discourse/app/lib/text.js | 2 +- .../app/lib/textarea-text-manipulation.js | 6 +- .../components/prosemirror-editor.gjs | 147 ++++++ .../extensions/code-lang-selector.js | 60 +++ .../static/prosemirror/extensions/emoji.js | 71 +++ .../static/prosemirror/extensions/hashtag.js | 60 +++ .../static/prosemirror/extensions/heading.js | 22 + .../prosemirror/extensions/html-inline.js | 92 ++++ .../static/prosemirror/extensions/image.js | 170 +++++++ .../static/prosemirror/extensions/index.js | 35 ++ .../app/static/prosemirror/extensions/link.js | 32 ++ .../static/prosemirror/extensions/mention.js | 65 +++ .../static/prosemirror/extensions/quote.js | 90 ++++ .../prosemirror/extensions/strikethrough.js | 30 ++ .../static/prosemirror/extensions/table.js | 176 +++++++ .../extensions/typographer-replacements.js | 33 ++ .../prosemirror/extensions/underline.js | 21 + .../app/static/prosemirror/lib/parser.js | 52 ++ .../app/static/prosemirror/lib/schema.js | 21 + .../app/static/prosemirror/lib/serializer.js | 31 ++ .../prosemirror/lib/text-manipulation.js | 446 ++++++++++++++++++ .../prosemirror/plugins/code-highlight.js | 13 + .../static/prosemirror/plugins/inputrules.js | 136 ++++++ .../app/static/prosemirror/plugins/keymap.js | 101 ++++ .../static/prosemirror/plugins/placeholder.js | 27 ++ app/assets/javascripts/discourse/package.json | 15 +- app/assets/stylesheets/common.scss | 1 + .../stylesheets/common/components/_index.scss | 1 + .../components/composer-toggle-switch.scss | 146 ++++++ .../common/rich-editor/_index.scss | 1 + .../common/rich-editor/rich-editor.scss | 198 ++++++++ app/assets/stylesheets/mobile/compose.scss | 1 + config/site_settings.yml | 4 + docs/CHANGELOG-JAVASCRIPT-PLUGIN-API.md | 4 + lib/svg_sprite.rb | 2 + .../initializers/chat-keyboard-shortcuts.js | 10 +- .../discourse/initializers/checklist.js | 2 + .../javascripts/lib/rich-editor-extension.js | 67 +++ .../assets/stylesheets/checklist.scss | 11 + .../javascripts/initializers/apply-details.js | 3 + .../javascripts/lib/rich-editor-extension.js | 41 ++ .../assets/stylesheets/details.scss | 3 +- .../initializers/discourse-local-dates.js | 3 + .../javascripts/lib/rich-editor-extension.js | 132 ++++++ 52 files changed, 2943 insertions(+), 55 deletions(-) create mode 100644 app/assets/javascripts/discourse/app/components/composer/toggle-switch.gjs create mode 100644 app/assets/javascripts/discourse/app/lib/composer/rich-editor-extensions.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/components/prosemirror-editor.gjs create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/extensions/code-lang-selector.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/extensions/emoji.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/extensions/hashtag.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/extensions/heading.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/extensions/html-inline.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/extensions/image.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/extensions/index.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/extensions/link.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/extensions/mention.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/extensions/quote.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/extensions/strikethrough.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/extensions/table.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/extensions/typographer-replacements.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/extensions/underline.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/lib/parser.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/lib/schema.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/lib/serializer.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/lib/text-manipulation.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/plugins/code-highlight.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/plugins/inputrules.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/plugins/keymap.js create mode 100644 app/assets/javascripts/discourse/app/static/prosemirror/plugins/placeholder.js create mode 100644 app/assets/stylesheets/common/components/composer-toggle-switch.scss create mode 100644 app/assets/stylesheets/common/rich-editor/_index.scss create mode 100644 app/assets/stylesheets/common/rich-editor/rich-editor.scss create mode 100644 plugins/checklist/assets/javascripts/lib/rich-editor-extension.js create mode 100644 plugins/discourse-details/assets/javascripts/lib/rich-editor-extension.js create mode 100644 plugins/discourse-local-dates/assets/javascripts/lib/rich-editor-extension.js diff --git a/app/assets/javascripts/discourse-markdown-it/src/features/custom-typographer-replacements.js b/app/assets/javascripts/discourse-markdown-it/src/features/custom-typographer-replacements.js index 6d29476d961..2a3d6bc1e05 100644 --- a/app/assets/javascripts/discourse-markdown-it/src/features/custom-typographer-replacements.js +++ b/app/assets/javascripts/discourse-markdown-it/src/features/custom-typographer-replacements.js @@ -14,9 +14,9 @@ // (r) (R) → ® // (p) (P) -> § -let RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,|--|-->|<--|->|<-|<->|<-->/; +export const RARE_RE = /\+-|\.\.|\?\?\?\?|!!!!|,,|--|-->|<--|->|<-|<->|<-->/; -let SCOPED_ABBR_RE = /\((tm|pa)\)/gi; +export const SCOPED_ABBR_RE = /\((tm|pa)\)/gi; let SCOPED_ABBR = { pa: "¶", tm: "™", @@ -32,11 +32,37 @@ function replaceScoped(inlineTokens) { for (i = inlineTokens.length - 1; i >= 0; i--) { token = inlineTokens[i]; if (token.type === "text") { - token.content = token.content.replace(SCOPED_ABBR_RE, replaceFn); + token.content = replaceScopedStr(token.content.replace); } } } +export function replaceScopedStr(str) { + return str.replace(SCOPED_ABBR_RE, replaceFn); +} + +export function replaceRareStr(str) { + return ( + str + .replace(/\+-/g, "±") + // Custom arrows + .replace(/(^|\s)-{1,2}>(\s|$)/gm, "\u0020\u2192\u0020") + .replace(/(^|\s)<-{1,2}(\s|$)/gm, "\u0020\u2190\u0020") + .replace(/(^|\s)<-{1,2}>(\s|$)/gm, "\u0020\u2194\u0020") + // .., ..., ....... -> … + // but ?..... & !..... -> ?.. & !.. + .replace(/\.{2,}/g, "…") + .replace(/([?!])…/g, "$1..") + .replace(/([?!]){4,}/g, "$1$1$1") + .replace(/,{2,}/g, ",") + // em-dash + .replace(/(^|[^-])---(?=[^-]|$)/gm, "$1\u2014") + // en-dash + .replace(/(^|\s)--(?=\s|$)/gm, "$1\u2013") + .replace(/(^|[^-\s])--(?=[^-\s]|$)/gm, "$1\u2013") + ); +} + function replaceRare(inlineTokens) { let i, token, @@ -47,23 +73,7 @@ function replaceRare(inlineTokens) { if (token.type === "text" && !inside_autolink) { if (RARE_RE.test(token.content)) { - token.content = token.content - .replace(/\+-/g, "±") - // Custom arrows - .replace(/(^|\s)-{1,2}>(\s|$)/gm, "\u0020\u2192\u0020") - .replace(/(^|\s)<-{1,2}(\s|$)/gm, "\u0020\u2190\u0020") - .replace(/(^|\s)<-{1,2}>(\s|$)/gm, "\u0020\u2194\u0020") - // .., ..., ....... -> … - // but ?..... & !..... -> ?.. & !.. - .replace(/\.{2,}/g, "…") - .replace(/([?!])…/g, "$1..") - .replace(/([?!]){4,}/g, "$1$1$1") - .replace(/,{2,}/g, ",") - // em-dash - .replace(/(^|[^-])---(?=[^-]|$)/gm, "$1\u2014") - // en-dash - .replace(/(^|\s)--(?=\s|$)/gm, "$1\u2013") - .replace(/(^|[^-\s])--(?=[^-\s]|$)/gm, "$1\u2013"); + token.content = replaceRareStr(token.content); } } diff --git a/app/assets/javascripts/discourse/app/components/composer-container.hbs b/app/assets/javascripts/discourse/app/components/composer-container.hbs index 21782830dca..8270b60d128 100644 --- a/app/assets/javascripts/discourse/app/components/composer-container.hbs +++ b/app/assets/javascripts/discourse/app/components/composer-container.hbs @@ -298,15 +298,17 @@ {{/if}} - - {{d-icon "desktop"}} - + {{#if this.composer.allowPreview}} + + {{d-icon "desktop"}} + + {{/if}} {{#if this.composer.showPreview}} - {{#if this.site.desktopView}} + {{#if (and this.composer.allowPreview this.site.desktopView)}} +
+ +
+; + +export default ComposerToggleSwitch; diff --git a/app/assets/javascripts/discourse/app/components/d-editor.hbs b/app/assets/javascripts/discourse/app/components/d-editor.hbs index f509d8e73a9..d09e11a3bb6 100644 --- a/app/assets/javascripts/discourse/app/components/d-editor.hbs +++ b/app/assets/javascripts/discourse/app/components/d-editor.hbs @@ -8,7 +8,14 @@ {{if this.isEditorFocused 'in-focus'}}" >