Skip to content

Commit 7df0289

Browse files
committed
Added and fixed plain text formating
1 parent 9e83dc0 commit 7df0289

File tree

5 files changed

+101
-20
lines changed

5 files changed

+101
-20
lines changed

src/App.css

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,47 @@ html,
2626

2727
.code-block-container {
2828
background-color: #e3e3e3;
29-
padding: 2px 6px;
29+
padding: 10px;
3030
border-radius: 4px;
3131
margin: 4px 0;
32-
white-space: pre-wrap;
33-
word-break: break-word;
32+
white-space: pre;
33+
overflow-x: auto;
3434
width: 100%;
3535
box-sizing: border-box;
3636
color: #000;
37+
font-family: monospace;
3738
}
3839

3940
.inline-code {
4041
background-color: #e3e3e3;
4142
padding: 2px 6px;
4243
border-radius: 3px;
43-
font-family: monospace;
44-
display: block;
45-
width: 100%;
44+
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "DejaVu Sans Mono", "Noto Sans Mono", "Courier New", monospace;
45+
display: inline;
46+
width: auto;
4647
box-sizing: border-box;
47-
margin: 4px 0;
48+
margin: 0;
4849
color: #000;
4950
}
5051

5152
.code-block {
5253
font-family: monospace;
5354
}
5455

56+
/* ASCII art blocks detected from paragraph content */
57+
.ascii-art {
58+
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "DejaVu Sans Mono", "Noto Sans Mono", "Courier New", monospace, "Noto Color Emoji", "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
59+
white-space: pre;
60+
margin: 4px 0;
61+
padding: 6px 8px;
62+
background: transparent;
63+
color: inherit;
64+
overflow-x: auto;
65+
line-height: 1.2;
66+
-webkit-font-smoothing: antialiased;
67+
-moz-osx-font-smoothing: grayscale;
68+
}
69+
5570
.textarea-horizontal-full {
5671
/* Base textarea styles */
5772
width: 98%;
@@ -100,7 +115,7 @@ html,
100115
.preview-parallel,
101116
.preview-horizontal-full {
102117
line-height: 1.5; /* Normal line height */
103-
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
118+
font-family: monospace;
104119
letter-spacing: normal;
105120
word-spacing: normal;
106121

@@ -110,19 +125,21 @@ html,
110125
text-rendering: optimizeLegibility;
111126
}
112127

128+
.preview-horizontal p,
129+
.preview-parallel p,
130+
.preview-horizontal-full p {
131+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
132+
white-space: pre-wrap;
133+
}
134+
113135
/* Apply emoji fonts only to elements that actually need them (emojis, icons) */
114136
.preview-horizontal .icon-glyph,
115137
.preview-parallel .icon-glyph,
116138
.preview-horizontal-full .icon-glyph {
117139
font-family: "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", sans-serif;
118140
}
119141

120-
/* Paragraph spacing */
121-
.preview-horizontal p,
122-
.preview-parallel p,
123-
.preview-horizontal-full p {
124-
margin: 0.5em 0;
125-
}
142+
126143

127144
/* Header spacing */
128145
.preview-horizontal h1, .preview-horizontal h2, .preview-horizontal h3,

src/App.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import {
6666
handleClear,
6767
handleRedo,
6868
handleOpenClick,
69+
handleOpenTxtClick,
6970
saveToHTML,
7071
saveToFile,
7172
saveToTxT
@@ -774,6 +775,17 @@ const App = () => {
774775
<div className="hdr-desc">Open markdown .md file</div>
775776
</button>
776777
<div className="hdr-sep" />
778+
<button
779+
className="dropdown-item"
780+
onClick={() => {
781+
handleOpenTxtClick(setEditorContent);
782+
setShowHelpDropdown(false);
783+
}}
784+
>
785+
<div className="hdr-title"><FaFileImport /> Open TXT</div>
786+
<div className="hdr-desc">Open plain text .txt file</div>
787+
</button>
788+
<div className="hdr-sep" />
777789
<button
778790
className="dropdown-item"
779791
onClick={() => {

src/components/PreviewComponent.tsx

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ const PreviewComponent: React.FC<PreviewComponentProps> = React.memo(({
9393
remarkPlugins={[remarkGfm, remarkEmoji]}
9494
rehypePlugins={[rehypeRaw]}
9595
components={{
96-
code({ className, children, ...props }) {
96+
code(_props) {
97+
const { className, children, ...props } = _props as any;
98+
const isInlineFlag = (_props as any)?.inline as boolean | undefined;
9799
const isMermaid = /language-mermaid/.test(className || "");
98100
const isPlantUML = /language-plantuml/.test(className || "");
99101

@@ -138,7 +140,8 @@ const PreviewComponent: React.FC<PreviewComponentProps> = React.memo(({
138140
}
139141
}
140142

141-
const isInline = !className;
143+
// ReactMarkdown provides `inline` flag to distinguish inline vs fenced code
144+
const isInline = !!isInlineFlag;
142145

143146
return (
144147
<code
@@ -149,6 +152,36 @@ const PreviewComponent: React.FC<PreviewComponentProps> = React.memo(({
149152
</code>
150153
);
151154
},
155+
p({ children }) {
156+
// Detect box-drawing characters so ASCII UI blocks render with fixed width
157+
const flatten = (nodes: any): string => {
158+
if (!nodes) return '';
159+
const arr = Array.isArray(nodes) ? nodes : [nodes];
160+
return arr
161+
.map((n) => {
162+
if (typeof n === 'string') return n;
163+
// Treat <br/> as newline
164+
// @ts-ignore
165+
if (n && n.type === 'br') return '\n';
166+
// @ts-ignore
167+
if (n && n.props && n.props.children) return flatten(n.props.children as any);
168+
return '';
169+
})
170+
.join('');
171+
};
172+
173+
const text = flatten(children);
174+
const hasBoxChars = /[\u2500-\u257F]/.test(text);
175+
const looksLikeAsciiArt = hasBoxChars && /\n/.test(text);
176+
177+
if (looksLikeAsciiArt) {
178+
return (
179+
<pre className="ascii-art">{text}</pre>
180+
);
181+
}
182+
183+
return <p>{children}</p>;
184+
},
152185
pre({ children }) {
153186
return (
154187
<pre className="code-block-container">

src/insertMermaid.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,6 @@ graph LR
106106
I -->|Ready| J[Product Manufacture]
107107
I -->|Not Ready| K[Delay in Production]
108108
K --> H
109-
110-
J --> L[Market Release]
111-
L -->|Approved| M[Launch Campaign]
112-
L -->|Not Approved| N[Postponement Decision]
113109
\`\`\``;
114110
const newText =
115111
editorContent.substring(0, start) +

src/insertSave.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,29 @@ export const handleOpenClick = (
146146
input.click();
147147
};
148148

149+
// Open a plain text (.txt) file and load its contents into the editor
150+
export const handleOpenTxtClick = (
151+
setEditorContent: (content: string) => void
152+
): void => {
153+
const input = document.createElement("input");
154+
input.type = "file";
155+
input.accept = ".txt,text/plain";
156+
input.onchange = (e) => {
157+
const file = (e.target as HTMLInputElement).files?.[0];
158+
if (file) {
159+
const reader = new FileReader();
160+
reader.onload = (e) => {
161+
const contents = e.target?.result;
162+
if (typeof contents === "string") {
163+
setEditorContent(contents);
164+
}
165+
};
166+
reader.readAsText(file);
167+
}
168+
};
169+
input.click();
170+
};
171+
149172
export const saveToFile = (editorContent: string): void => {
150173
const blob = new Blob([editorContent], { type: "text/markdown;charset=utf-8" });
151174
saveAs(blob, "easyedit.md");

0 commit comments

Comments
 (0)