mirror of
https://github.com/chatwoot/chatwoot.git
synced 2026-06-04 21:02:35 +08:00
# Pull Request Template ## Description This PR adds support for inline image uploads in the reply editor for Email and Website (chat widget) channels. Agents can now insert images inline between text and resize them directly in the editor by dragging the bottom corner, similar to the help center editor experience. Image sizes are preserved through markdown using the `cw_image_width` URL param and render correctly in both outgoing emails and chat widget messages. Agents can also paste copied images directly into Email or Website replies using **Shift+Cmd+V** (Shift+Ctrl+V on Windows/Linux). The image gets inserted inline at the cursor position and supports resizing just like uploaded images. Regular **Cmd+V / Ctrl+V** behavior remains unchanged and continues to add images as attachments, so both inline and attachment flows are supported. ### Prosemirror repo PR: https://github.com/chatwoot/prosemirror-schema/pull/48 Fixes https://linear.app/chatwoot/issue/CW-7133/inline-images-in-live-chat-and-email https://linear.app/chatwoot/issue/CW-7225/ghsa-8j9w-jppp-xcfc-html-attribute-injection-via-unvalidated-cw-image ## Type of change - [x] New feature (non-breaking change which adds functionality) ## How Has This Been Tested? ### Screencast https://github.com/user-attachments/assets/a928f852-ab15-413a-9d35-6ea69b718ecf <img width="414" height="654" alt="image" src="https://github.com/user-attachments/assets/205e0729-8f2d-4cc5-9c55-7696f032eca4" /> ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Muhsin Keloth <[email protected]>
114 lines
3.0 KiB
JavaScript
114 lines
3.0 KiB
JavaScript
import MarkdownIt from 'markdown-it';
|
|
import mila from 'markdown-it-link-attributes';
|
|
import mentionPlugin from './markdownIt/link';
|
|
|
|
const setImageSizing = inlineToken => {
|
|
const imgSrc = inlineToken.attrGet('src');
|
|
if (!imgSrc) return;
|
|
const url = new URL(imgSrc);
|
|
const width = url.searchParams.get('cw_image_width');
|
|
if (width) {
|
|
inlineToken.attrSet(
|
|
'style',
|
|
`width: ${width}; max-width: 100%; height: auto;`
|
|
);
|
|
return;
|
|
}
|
|
const height = url.searchParams.get('cw_image_height');
|
|
if (height) inlineToken.attrSet('style', `height: ${height};`);
|
|
};
|
|
|
|
const processInlineToken = blockToken => {
|
|
blockToken.children.forEach(inlineToken => {
|
|
if (inlineToken.type === 'image') {
|
|
setImageSizing(inlineToken);
|
|
}
|
|
});
|
|
};
|
|
|
|
const imgResizeManager = md => {
|
|
// If the image URL carries a cw_image_width or cw_image_height query param,
|
|
// add an inline style attribute so the rendered <img> respects the agent's
|
|
// resize choice. Width takes precedence (HC drag-resize); height is kept for
|
|
// legacy messages and the message-signature use case.
|
|
md.core.ruler.after('inline', 'add-image-sizing', state => {
|
|
state.tokens.forEach(blockToken => {
|
|
if (blockToken.type === 'inline') {
|
|
processInlineToken(blockToken);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
const createMarkdownInstance = (linkify = true) => {
|
|
return MarkdownIt({
|
|
html: false,
|
|
xhtmlOut: true,
|
|
breaks: true,
|
|
langPrefix: 'language-',
|
|
linkify,
|
|
typographer: true,
|
|
quotes: '\u201c\u201d\u2018\u2019',
|
|
maxNesting: 20,
|
|
})
|
|
.disable(['lheading'])
|
|
.use(mentionPlugin)
|
|
.use(imgResizeManager)
|
|
.use(mila, {
|
|
attrs: {
|
|
class: 'link',
|
|
rel: 'noreferrer noopener nofollow',
|
|
target: '_blank',
|
|
},
|
|
});
|
|
};
|
|
|
|
const TWITTER_USERNAME_REGEX = /(^|[^@\w])@(\w{1,15})\b/g;
|
|
const TWITTER_USERNAME_REPLACEMENT = '$1[@$2](http://twitter.com/$2)';
|
|
const TWITTER_HASH_REGEX = /(^|\s)#(\w+)/g;
|
|
const TWITTER_HASH_REPLACEMENT = '$1[#$2](https://twitter.com/hashtag/$2)';
|
|
|
|
class MessageFormatter {
|
|
constructor(
|
|
message,
|
|
isATweet = false,
|
|
isAPrivateNote = false,
|
|
linkify = true
|
|
) {
|
|
this.message = message || '';
|
|
this.isAPrivateNote = isAPrivateNote;
|
|
this.isATweet = isATweet;
|
|
this.linkify = linkify;
|
|
this.md = createMarkdownInstance(linkify);
|
|
}
|
|
|
|
formatMessage() {
|
|
let updatedMessage = this.message;
|
|
if (this.isATweet && !this.isAPrivateNote) {
|
|
updatedMessage = updatedMessage.replace(
|
|
TWITTER_USERNAME_REGEX,
|
|
TWITTER_USERNAME_REPLACEMENT
|
|
);
|
|
updatedMessage = updatedMessage.replace(
|
|
TWITTER_HASH_REGEX,
|
|
TWITTER_HASH_REPLACEMENT
|
|
);
|
|
}
|
|
return this.md.render(updatedMessage);
|
|
}
|
|
|
|
get formattedMessage() {
|
|
return this.formatMessage();
|
|
}
|
|
|
|
get plainText() {
|
|
const strippedOutHtml = new DOMParser().parseFromString(
|
|
this.formattedMessage,
|
|
'text/html'
|
|
);
|
|
return strippedOutHtml.body.textContent || '';
|
|
}
|
|
}
|
|
|
|
export default MessageFormatter;
|