Skip to content

Commit

Permalink
Support bold & italic & underline & strikethrough
Browse files Browse the repository at this point in the history
  • Loading branch information
boybook committed Sep 8, 2023
1 parent c6584ad commit 5d61c79
Showing 1 changed file with 135 additions and 41 deletions.
176 changes: 135 additions & 41 deletions src/components/MCTextFormatter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,36 @@
<textarea v-model="rawText" @input="formatText" ref="textarea"></textarea>
<div>
<format-button
v-for="(color, format) in colors"
v-for="(color, format) in colorsFormats"
v-bind:format="format"
v-bind:color="color"
v-bind:key="color"
@format="insertFormat"
>
{{ format }}
</format-button>
<FormatButton v-bind:format="'§l'" v-bind:bold="true" @format="insertFormat">粗§l</FormatButton>
<FormatButton v-bind:format="'§o'" v-bind:italic="true" @format="insertFormat">斜§o</FormatButton>
<FormatButton v-bind:format="'§r'" @format="insertFormat">复原§r</FormatButton>
<format-button v-if="!braceMode" v-bind:format="'§l'" v-bind:bold="true" @format="insertFormat">粗§l</format-button>
<format-button v-if="!braceMode" v-bind:format="'§o'" v-bind:italic="true" @format="insertFormat">斜§o</format-button>
<format-button v-if="!braceMode" v-bind:format="'§n'" v-bind:underline="true" @format="insertFormat">下划线§n</format-button>
<format-button v-if="!braceMode" v-bind:format="'§m'" v-bind:strikethrough="true" @format="insertFormat">删除线§m</format-button>
<format-button v-if="!braceMode" v-bind:format="'§r'" @format="insertFormat">复原§r</format-button>
<format-button v-if="braceMode" v-bind:format="'{bold}'" v-bind:bold="true" @format="insertFormat">粗{bold}</format-button>
<format-button v-if="braceMode" v-bind:format="'{italic}'" v-bind:italic="true" @format="insertFormat">斜{italic}</format-button>
<format-button v-if="braceMode" v-bind:format="'{reset}'" @format="insertFormat">复原{reset}</format-button>
</div>
<div :class="['output', { 'dark-mode': isDarkMode }]" v-html="formattedText"></div>
</div>
<div class="wrapper-bottom">
<label class="small-gray-text">
<input type="checkbox" v-model="isDarkMode" @change="toggleDarkMode">
暗色背景
</label>
<div class="settings">
<label class="small-gray-text">
<input type="checkbox" v-model="isDarkMode" @change="toggleDarkMode">
暗色背景
</label>
<label class="small-gray-text">
<input type="checkbox" v-model="braceMode">
大括号模式
</label>
</div>
<div class="github-link">
<a href="https://github.com/EaseCation/mc-text-formatter" target="_blank">
<svg width="12" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!--! Font Awesome Pro 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg>
Expand All @@ -32,7 +43,7 @@
</template>

<script>
import { ref, onMounted, watch } from 'vue';
import { ref, onMounted, watch, computed } from 'vue';
import FormatButton from './FormatButton.vue';
export default {
Expand All @@ -41,6 +52,61 @@ export default {
setup() {
const braceMode = ref(false)
const colorsFormats = computed(() => {
if (braceMode.value) {
return {
"{black}": "#000000",
"{dark-blue}": "#0000AA",
"{dark-green}": "#00AA00",
"{dark-aqua}": "#00AAAA",
"{dark-red}": "#AA0000",
"{dark-purple}": "#AA00AA",
"{gold}": "#FFAA00",
"{gray}": "#AAAAAA",
"{dark-gray}": "#555555",
"{blue}": "#5555FF",
"{green}": "#55FF55",
"{aqua}": "#55FFFF",
"{red}": "#FF5555",
"{light-purple}": "#FF55FF",
"{yellow}": "#FFFF55",
"{white}": "#FFFFFF"
};
} else {
return {
"§0": "#000000",
"§1": "#0000AA",
"§2": "#00AA00",
"§3": "#00AAAA",
"§4": "#AA0000",
"§5": "#AA00AA",
"§6": "#FFAA00",
"§7": "#AAAAAA",
"§8": "#555555",
"§9": "#5555FF",
"§a": "#55FF55",
"§b": "#55FFFF",
"§c": "#FF5555",
"§d": "#FF55FF",
"§e": "#FFFF55",
"§f": "#FFFFFF",
"§g": "#DDD605",
"§h": "#E3D4D1",
"§i": "#CECACA",
"§j": "#443A3B",
"§m": "#971607",
"§n": "#B4684D",
"§p": "#DEB12D",
"§q": "#11A036",
"§s": "#2CBAA8",
"§t": "#21497B",
"§u": "#9A5CC6"
};
}
})
const colors = ref({
"§0": "#000000",
"§1": "#0000AA",
Expand Down Expand Up @@ -137,10 +203,16 @@ export default {
formatted = formatted.replace(regex, replacement);
}
const boldRegex = /§l(.*?)(?=[0-9a-u]|§r|\n|$))/g;
const boldRegex = /§l(.*?)(?=§r|\n|$)/g;
formatted = formatted.replace(boldRegex, `<strong>$1</strong>`);
const italicRegex = /§o(.*?)(?=(§[0-9a-u]|§r|\n|$))/g;
const underlineRegex = /§n(.*?)(?=§r|\n|$)/g;
formatted = formatted.replace(underlineRegex, `<u>$1</u>`);
const strikethroughRegex = /§m(.*?)(?=§r|\n|$)/g;
formatted = formatted.replace(strikethroughRegex, `<s>$1</s>`);
const italicRegex = /§o(.*?)(?=§r|\n|$)/g;
formatted = formatted.replace(italicRegex, `<em>$1</em>`);
for (let code in colors.value) {
Expand All @@ -159,6 +231,8 @@ export default {
};
return {
braceMode,
colorsFormats,
colors,
colorsBrace,
rawText,
Expand Down Expand Up @@ -204,6 +278,15 @@ body {
align-items: center;
}
.wrapper-bottom .settings {
display: flex;
justify-content: center;
padding: 0;
flex-direction: row;
align-items: center;
gap: 16px;
}
textarea {
width: 100%;
height: 150px;
Expand All @@ -223,11 +306,6 @@ textarea {
background-color: white;
}
.output span {
font-weight: normal;
font-style: normal;
}
.output.dark-mode {
background-color: #333;
color: #fff;
Expand All @@ -254,31 +332,6 @@ button:hover {
color: #888; /* 设置为浅灰色,可以根据需要调整颜色 */
}
/* 暗黑模式 */
@media (prefers-color-scheme: dark) {
body {
color: #fff;
background-color: #222;
}
.wrapper {
background-color: #444;
}
textarea {
background-color: #555;
color: #fff;
border-color: #666;
}
}
/* 手机适配 */
@media (max-width: 768px) {
.wrapper {
margin: 4px;
}
}
.github-link {
margin-top: 20px;
text-align: center;
Expand Down Expand Up @@ -307,4 +360,45 @@ button:hover {
fill: rgba(0, 0, 0, 0.85);
}
/* 暗黑模式 */
@media (prefers-color-scheme: dark) {
body {
color: #fff;
background-color: #222;
}
.wrapper {
background-color: #444;
}
textarea {
background-color: #555;
color: #fff;
border-color: #666;
}
.github-link a {
color: rgba(255, 255, 255, 0.45);
}
.github-link a svg {
fill: rgba(255, 255, 255, 0.45);
}
.github-link a:hover {
color: rgba(255, 255, 255, .85)
}
.github-link a:hover svg {
fill: rgba(255, 255, 255, 0.85);
}
}
/* 手机适配 */
@media (max-width: 768px) {
.wrapper {
margin: 4px;
}
}
</style>

0 comments on commit 5d61c79

Please sign in to comment.