| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- <template>
- <view>
- <view
- class="dialogMask"
- v-if="isShow"
- :style="{
- height: height + 'px',
- }"
- >
- <view
- class="dialogContent"
- @click.stop
- >
- <view
- class="dialogHead"
- @click="hide"
- >
- <view>
- <text class="title">{{ title }}</text>
- </view>
- <view>
- <image
- src="@/devTools/page/static/unfold.png"
- class="fold"
- />
- </view>
- </view>
- <scroll-view
- scroll-y
- class="scrollList"
- :style="{
- height: dialogHeight + 'px',
- }"
- >
- <textarea
- :style="{
- height: dialogHeight - (canSave ? 90 : 40) + 'px',
- }"
- v-model="value"
- type="text"
- placeholder="请输入..."
- class="fileEditInput"
- maxlength="-1"
- />
- <view
- class="saveBtn"
- v-if="canSave"
- @click="saveFile"
- >
- <text class="saveBtnText">保存</text>
- </view>
- </scroll-view>
- </view>
- </view>
- </view>
- </template>
- <script>
- let success, error;
- export default {
- data() {
- return {
- /**
- * 是否展示
- */
- isShow: false,
- /**
- * 屏幕高度
- */
- height: uni.getSystemInfoSync().windowHeight,
- dialogHeight: uni.getSystemInfoSync().windowHeight * 0.8,
- /**
- * 弹窗标题
- */
- title: "",
- /**
- * 文本内容
- */
- value: "",
- /**
- * 是否为文件编辑模式
- */
- isFileEdit: true,
- /**
- * 文件路径
- */
- path: "",
- /**
- * 是否允许保存
- */
- canSave: false,
- /**
- * 是否为新建文件
- */
- isNewFile: false,
- };
- },
- mounted() {
- let that = this;
- uni.$on("devTools_showTextEditDialog", (options) => {
- that
- .show(options)
- .then((val) => {
- uni.$emit("devTools_showTextEditDialogSave", val);
- })
- .catch(() => {
- uni.$emit("devTools_showTextEditDialogHide");
- });
- });
- },
- unmounted() {
- uni.$off("devTools_showTextEditDialog");
- },
- methods: {
- /**
- * 展示弹窗
- */
- show(options) {
- let that = this;
- return new Promise((yes, err) => {
- success = yes;
- error = err;
- that.title = options.title;
- that.canSave = Boolean(options.canSave);
- that.isShow = true;
- if (options.isFileEdit === false) {
- // 仅为文件编辑模式
- that.isFileEdit = false;
- try {
- that.value = JSON.stringify(JSON.parse(options.value), null, 2);
- } catch (error) {
- that.value = options.value;
- }
- return;
- }
- that.isFileEdit = true;
- that.value = "文件读取中...";
- that.path = options.path;
- that.isNewFile = Boolean(options.isNewFile);
- // #ifdef APP-PLUS
- if (that.isNewFile) {
- that.value = "";
- } else {
- plus.io.resolveLocalFileSystemURL(
- that.path,
- (entry) => {
- // 可通过entry对象操作test.html文件
- entry.file((file) => {
- var fileReader = new plus.io.FileReader();
- fileReader.readAsText(file, "utf-8");
- fileReader.onloadend = function (evt) {
- let res = "";
- try {
- res = JSON.stringify(JSON.parse(evt.target.result), null, 2);
- } catch (error) {
- res = evt.target.result;
- }
- that.value = res;
- };
- fileReader.onerror = function () {
- that.value = `[${that.path}]文件读取失败!_code_2`;
- };
- });
- },
- function (e) {
- that.value = `[${that.path}]文件读取失败!` + e.message;
- }
- );
- }
- // #endif
- // #ifdef MP-WEIXIN
- let fs = wx.getFileSystemManager();
- if (options.size != 0) {
- fs.readFile({
- filePath: that.path,
- encoding: "utf8",
- position: 0,
- length: options.size,
- success({ data }) {
- try {
- that.value = JSON.stringify(JSON.parse(data), null, 2);
- } catch (error) {
- that.value = data;
- }
- },
- fail(e) {
- console.log(e);
- that.value = `[${that.path}]文件读取失败!` + e;
- },
- });
- } else {
- that.value = "";
- }
- // #endif
- });
- },
- /**
- * 关闭弹窗
- */
- hide() {
- this.isShow = false;
- error();
- },
- /**
- * 保存
- */
- save() {
- this.isShow = false;
- success(this.value);
- },
- /**
- * 保存文件
- */
- saveFile() {
- let that = this;
- if (!that.isFileEdit) {
- // 非文件编辑模式
- that.isShow = false;
- success(that.value);
- return;
- }
- uni.showLoading({
- title: "保存中",
- });
- // #ifdef APP-PLUS
- let fileName = that.path.split("/").pop();
- let path = that.path.substring(0, that.path.length - fileName.length);
- plus.io.resolveLocalFileSystemURL(
- path,
- (entry) => {
- entry.getFile(
- fileName,
- {
- create: true,
- },
- (fileEntry) => {
- fileEntry.createWriter((writer) => {
- writer.onwrite = (e) => {
- uni.hideLoading();
- uni.showToast({
- title: "文件保存成功!",
- icon: "success",
- });
- that.isShow = false;
- that.$emit("getPage");
- };
- writer.onerror = () => {
- uni.hideLoading();
- uni.showToast({
- title: "文件保存失败!_写入文件失败",
- icon: "none",
- });
- };
- writer.write(that.value);
- });
- }
- );
- },
- () => {
- uni.hideLoading();
- uni.showToast({
- title: "文件保存失败!_打开目录失败",
- icon: "none",
- });
- }
- );
- // #endif
- // #ifdef MP-WEIXIN
- let fs = wx.getFileSystemManager();
- fs.writeFile({
- filePath: that.path,
- encoding: "utf-8",
- data: that.value,
- success() {
- uni.hideLoading();
- uni.showToast({
- title: "文件保存成功!",
- icon: "success",
- });
- that.isShow = false;
- that.$emit("getPage");
- },
- fail() {
- uni.hideLoading();
- uni.showToast({
- title: "文件保存失败!_打开目录失败",
- icon: "none",
- });
- },
- });
- // #endif
- },
- },
- };
- </script>
- <style lang="scss" scoped>
- .dialogMask {
- display: flex;
- flex-direction: column-reverse;
- background-color: rgba(0, 0, 0, 0.3);
- width: 750rpx;
- flex: 1;
- /* #ifndef APP-PLUS */
- height: 100vh;
- backdrop-filter: blur(1px);
- /* #endif */
- position: fixed;
- left: 0;
- top: 0;
- z-index: 999;
- .dialogContent {
- display: flex;
- flex-direction: column;
- width: 750rpx;
- background-color: #fff;
- border-radius: 20rpx 20rpx 0 0;
- .dialogHead {
- display: flex;
- flex-direction: row;
- align-items: center;
- justify-content: space-between;
- height: 80rpx;
- border-bottom: 1px solid rgba(0, 0, 0, 0.05);
- width: 750rpx;
- .title {
- margin-left: 20rpx;
- font-size: 24rpx;
- line-height: 24rpx;
- color: #333;
- }
- .fold {
- width: 20rpx;
- height: 20rpx;
- margin-right: 20rpx;
- }
- }
- .scrollList {
- width: 750rpx;
- padding: 20rpx;
- .fileEditInput {
- font-size: 20rpx;
- }
- .saveBtn {
- display: flex;
- flex-direction: row;
- align-items: center;
- justify-content: center;
- margin-top: 20rpx;
- height: 35px;
- width: 710rpx;
- border-radius: 10rpx;
- background-color: #ff2d55;
- .saveBtnText {
- color: #fff;
- font-size: 24rpx;
- line-height: 24rpx;
- }
- }
- }
- }
- }
- </style>
|