mirror of
https://github.com/lingble/chatwoot.git
synced 2025-10-30 18:47:51 +00:00
Fixes https://github.com/chatwoot/chatwoot/issues/8436 Fixes https://github.com/chatwoot/chatwoot/issues/9767 Fixes https://github.com/chatwoot/chatwoot/issues/10156 Fixes https://github.com/chatwoot/chatwoot/issues/6031 Fixes https://github.com/chatwoot/chatwoot/issues/5696 Fixes https://github.com/chatwoot/chatwoot/issues/9250 Fixes https://github.com/chatwoot/chatwoot/issues/9762 --------- Co-authored-by: Pranav <pranavrajs@gmail.com> Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
83 lines
2.2 KiB
Vue
83 lines
2.2 KiB
Vue
<script>
|
|
import 'highlight.js/styles/default.css';
|
|
import 'highlight.js/lib/common';
|
|
|
|
import { copyTextToClipboard } from 'shared/helpers/clipboard';
|
|
import { useAlert } from 'dashboard/composables';
|
|
|
|
export default {
|
|
props: {
|
|
script: {
|
|
type: String,
|
|
default: '',
|
|
},
|
|
lang: {
|
|
type: String,
|
|
default: 'javascript',
|
|
},
|
|
enableCodePen: {
|
|
type: Boolean,
|
|
default: false,
|
|
},
|
|
codepenTitle: {
|
|
type: String,
|
|
default: 'Chatwoot Codepen',
|
|
},
|
|
},
|
|
computed: {
|
|
codepenScriptValue() {
|
|
const lang = this.lang === 'javascript' ? 'js' : this.lang;
|
|
return JSON.stringify({
|
|
title: this.codepenTitle,
|
|
private: true,
|
|
[lang]: this.scrubbedScript,
|
|
});
|
|
},
|
|
scrubbedScript() {
|
|
// remove trailing and leading extra lines and not spaces
|
|
const scrubbed = this.script.replace(/^\s*[\r\n]/gm, '');
|
|
const lines = scrubbed.split('\n');
|
|
|
|
// remove extra indentations
|
|
const minIndent = lines.reduce((min, line) => {
|
|
if (line.trim().length === 0) return min;
|
|
const indent = line.match(/^\s*/)[0].length;
|
|
return Math.min(min, indent);
|
|
}, Infinity);
|
|
|
|
return lines.map(line => line.slice(minIndent)).join('\n');
|
|
},
|
|
},
|
|
methods: {
|
|
async onCopy(e) {
|
|
e.preventDefault();
|
|
await copyTextToClipboard(this.scrubbedScript);
|
|
useAlert(this.$t('COMPONENTS.CODE.COPY_SUCCESSFUL'));
|
|
},
|
|
},
|
|
};
|
|
</script>
|
|
|
|
<template>
|
|
<div class="relative text-left">
|
|
<div class="top-1.5 absolute right-1.5 flex items-center gap-1">
|
|
<form
|
|
v-if="enableCodePen"
|
|
class="flex items-center"
|
|
action="https://codepen.io/pen/define"
|
|
method="POST"
|
|
target="_blank"
|
|
>
|
|
<input type="hidden" name="data" :value="codepenScriptValue" />
|
|
<button type="submit" class="button secondary tiny">
|
|
{{ $t('COMPONENTS.CODE.CODEPEN') }}
|
|
</button>
|
|
</form>
|
|
<button type="button" class="button secondary tiny" @click="onCopy">
|
|
{{ $t('COMPONENTS.CODE.BUTTON_TEXT') }}
|
|
</button>
|
|
</div>
|
|
<highlightjs v-if="script" :language="lang" :code="scrubbedScript" />
|
|
</div>
|
|
</template>
|