Skip to content

Commit

Permalink
feat: add support for vertical & horizontal padding
Browse files Browse the repository at this point in the history
  • Loading branch information
BartoszGrajdek committed Oct 1, 2024
1 parent 5eb25b3 commit 457ea25
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/MarkdownTextInputDecoratorViewNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ interface MarkdownStyle {
borderColor: string;
borderRadius: Float;
padding: Float;
paddingVertical?: Float;
paddingHorizontal?: Float;
};
mentionHere: {
color: ColorValue;
Expand Down
1 change: 0 additions & 1 deletion src/web/MarkdownTextInput.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
position: absolute;
z-index: -1;
left: 0;
max-width: calc(100% + 2px);
box-sizing: border-box;
}

Expand Down
7 changes: 6 additions & 1 deletion src/web/utils/blockUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import type {NodeType, TreeNode} from './treeUtils';

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

const defaultPrePadding = markdownStyle.pre?.padding ?? 2;
const preHorizontalPadding = parseStringWithUnitToNumber(markdownStyle.pre?.paddingHorizontal ?? defaultPrePadding).toString();
const preVerticalPadding = parseStringWithUnitToNumber(markdownStyle.pre?.paddingVertical ?? defaultPrePadding).toString();

switch (type) {
case 'line':
Object.assign(node.style, {
Expand Down Expand Up @@ -58,7 +63,7 @@ function addStyleToBlock(targetElement: HTMLElement, type: NodeType, markdownSty
padding: 0,
});
Object.assign((node.parentNode as HTMLElement).style, {
padding: `${parseStringWithUnitToNumber(markdownStyle.pre?.padding ?? 2).toString()}px`,
padding: `${preVerticalPadding}px ${preHorizontalPadding}px`,
});
break;

Expand Down
30 changes: 24 additions & 6 deletions src/web/utils/webStyleUtils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type {TextStyle} from 'react-native';
import type {MarkdownStyle} from '../../MarkdownTextInputDecoratorViewNativeComponent';
import {mergeMarkdownStyleWithDefault} from '../../styleUtils';
import {mergeMarkdownStyleWithDefault, parseStringWithUnitToNumber} from '../../styleUtils';
import type {PartialMarkdownStyle} from '../../styleUtils';
import type {MarkdownTextInputElement} from '../../MarkdownTextInput.web';

Expand Down Expand Up @@ -101,6 +101,10 @@ function addStylesheetRules(rules: Rule[], styleSheet: CSSStyleSheet) {
});
}

function property(e: HTMLElement, p: string) {
return parseFloat(window.getComputedStyle(e).getPropertyValue(p).replace('px', ''));
}

function generateCodeBlocksRules(target: MarkdownTextInputElement, styleTag: HTMLStyleElement, markdownStyle: PartialMarkdownStyle) {
const line = target.querySelector('*[data-type="line"]:has(> *[data-type="pre"]) > span:first-child');
if (!line) {
Expand All @@ -109,35 +113,49 @@ function generateCodeBlocksRules(target: MarkdownTextInputElement, styleTag: HTM

const lineHeight = line.getBoundingClientRect()?.height;
const preStyles = markdownStyle.pre;
const padding = preStyles?.padding ?? 3;
const padding = preStyles?.padding ?? 2;
const horizontalPadding = parseStringWithUnitToNumber(preStyles?.paddingHorizontal ?? padding);
const verticalPadding = parseStringWithUnitToNumber(preStyles?.paddingVertical ?? padding);

const contentWidth =
target.offsetWidth - property(target, 'border-left-width') - property(target, 'border-left-width') - property(target, 'padding-left') - property(target, 'padding-right');

const rules: Rule[] = [
{
selector: `.${target.uniqueId} *[data-type='pre']::before`,
properties: {
top: `${Math.floor(lineHeight)}px`,
padding: `${(padding - 2)?.toString()}px`,
padding: `${verticalPadding.toString()}px ${horizontalPadding.toString()}px`,
'background-color': `${(preStyles?.backgroundColor as string) ?? 'lightgray'}`,
'border-radius': `${preStyles?.borderRadius?.toString() ?? '4'}px`,
'border-color': `${preStyles?.borderColor ?? 'grey'}`,
'min-width': `min(calc(100% + 1.5px), ${contentWidth}px)`,
'max-width': `${contentWidth}px`,
},
},
{
selector: `.${target.uniqueId} *[data-type='line'] *[data-type='syntax']:has(+ *[data-type='pre'])`,
properties: {
transform: `translate(-${padding}px, -${padding}px)`,
transform: `translate(-${horizontalPadding}px, -${verticalPadding}px)`,
},
},
{
selector: `.${target.uniqueId} *[data-type='line'] *[data-type='pre'] + *[data-type='syntax']`,
properties: {
transform: `translate(-${padding}px, ${padding}px)`,
transform: `translate(-${horizontalPadding}px, ${verticalPadding}px)`,
},
},
{
selector: `.${target.uniqueId} *[data-type='line'] *[data-type='pre'] + *[data-type='syntax'] + *[data-type='text']`,
properties: {
transform: `translate(-${padding}px, ${padding}px)`,
transform: `translate(-${horizontalPadding}px, ${verticalPadding}px)`,
},
},
{
selector: `.${target.uniqueId} *[data-type='line']:has(> *[data-type='pre']) > *:nth-child(n+3)`,
properties: {
display: 'inline-block',
transform: `translate(-${horizontalPadding}px, ${verticalPadding}px)`,
},
},
];
Expand Down

0 comments on commit 457ea25

Please sign in to comment.