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

Mention background alignment #594

Merged
merged 3 commits into from
Jan 21, 2025
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
6 changes: 5 additions & 1 deletion src/web/MarkdownTextInput.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
display: none;
}

.react-native-live-markdown-input-singleline span[data-type="text"] {
.react-native-live-markdown-input-singleline span[data-type='text'] {
vertical-align: middle;
}

.react-native-live-markdown-input-singleline > p:last-child {
padding-right: 1px !important;
}

.react-native-live-markdown-input-singleline:empty::before,
.react-native-live-markdown-input-multiline:empty::before {
pointer-events: none;
Expand Down
9 changes: 8 additions & 1 deletion src/web/utils/blockUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import type {PartialMarkdownStyle} from '../../styleUtils';
import {addInlineImagePreview} from '../inputElements/inlineImage';
import type {NodeType, TreeNode} from './treeUtils';

function addStyleToBlock(targetElement: HTMLElement, type: NodeType, markdownStyle: PartialMarkdownStyle) {
function addStyleToBlock(targetElement: HTMLElement, type: NodeType, markdownStyle: PartialMarkdownStyle, isMultiline = true) {
const node = targetElement;

switch (type) {
case 'line':
Object.assign(node.style, {
Expand Down Expand Up @@ -74,6 +75,12 @@ function addStyleToBlock(targetElement: HTMLElement, type: NodeType, markdownSty
position: 'relative',
});
break;
case 'text':
if (!isMultiline) {
Object.assign(node.style, {backgroundColor: targetElement.parentElement?.style.backgroundColor});
Object.assign(targetElement.parentElement?.style ?? {}, {backgroundColor: 'transparent'});
}
break;
default:
break;
}
Expand Down
12 changes: 9 additions & 3 deletions src/web/utils/parserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function addBrElement(node: TreeNode) {
return spanNode;
}

function addTextToElement(node: TreeNode, text: string) {
function addTextToElement(node: TreeNode, text: string, isMultiline = true) {
const lines = text.split('\n');
lines.forEach((line, index) => {
if (line !== '') {
Expand All @@ -96,6 +96,12 @@ function addTextToElement(node: TreeNode, text: string) {
span.setAttribute('data-type', 'text');
span.appendChild(document.createTextNode(line));
appendNode(span, node, 'text', line.length);

const parentType = span.parentElement?.dataset.type;
if (!isMultiline && parentType && ['pre', 'code', 'mention-here', 'mention-user', 'mention-report'].includes(parentType)) {
// this is a fix to background colors being shifted downwards in a singleline input
addStyleToBlock(span, 'text', {}, false);
}
}

if (index < lines.length - 1 || (index === 0 && line === '')) {
Expand Down Expand Up @@ -208,7 +214,7 @@ function parseRangesToHTMLNodes(
span.setAttribute('data-type', range.type);

if (!disableInlineStyles) {
addStyleToBlock(span, range.type, markdownStyle);
addStyleToBlock(span, range.type, markdownStyle, isMultiline);
}

const spanNode = appendNode(span, currentParentNode, range.type, range.length);
Expand All @@ -223,7 +229,7 @@ function parseRangesToHTMLNodes(
lastRangeEndIndex = range.start;
} else {
// adding markdown tag
addTextToElement(spanNode, text.substring(range.start, endOfCurrentRange));
addTextToElement(spanNode, text.substring(range.start, endOfCurrentRange), isMultiline);
currentParentNode.element.value = (currentParentNode.element.value || '') + (spanNode.element.value || '');
lastRangeEndIndex = endOfCurrentRange;
// tag unnesting and adding text after the tag
Expand Down
Loading