Ldc
2024-04-07 0652600959e5e3b5796fb6e8da129704ca95347a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
 * markdown编辑器页面
 * @author weidy
 * @date 2021-11-29
 */
layui.define(['layer','element','form','table','dynamicCondition','upload' ],function(exports){
    var webUtil = $webUtil;
    var Class = function(){
        this.MODELNAME = "platform/objectService/markdown";
        this.moduleKey = "markdown";
        this.backPath =  configData.adminServicePath;
        this.url = {
            controller:'helpDocumentController/',
            createOrGetContentByOid:'createOrGetContentByOid',
            saveContentByOid:'saveContentByOid',
            uploadPic:'uploadPic',
            gridImageInfo:'gridImageInfo'
        };
        this.getContent=function(){
            var that = this;
            var html = "";
            html = [
                '<div class="layui-layout" style="display:block;overflow-y: hidden">',
                    '<div class="layui-layout-border" style="display:block;margin-top:0px; " id="border_',that.id,'">',
                        '<div class="layui-center" style="overflow-y:auto;">',
                            this.getmarkdownHtml(),
                        '</div>',
                    '</div>',
                '</div>'
            ].join("");
            return html;
        };
        this.getmarkdownHtml=function (){
            var that=this;
            return '    <link href="style/css/markdown.min.css" rel="stylesheet" type="text/css"/>' +
                '<style type="text/css">\n' +
                '        html, body {\n' +
                '            margin: 0;\n' +
                '            padding: 0;\n' +
                '            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;\n' +
                '            color: #333;\n' +
                '            background-color: #fbfbfb;\n' +
                '            height: 100%;\n' +
                '        }\n' +
                '        textarea {\n' +
                '            font-family: Menlo, Monaco, Consolas, "Courier New", monospace;\n' +
                '            font-size: 12px;\n' +
                '            resize: none;\n' +
                '        }\n' +
                '        header {\n' +
                '            padding-top: 10px;\n' +
                '            display: flex;\n' +
                '            height: 58px;\n' +
                '        }\n' +
                '        header h1 {\n' +
                '            margin: 0;\n' +
                '        }\n' +
                '        .containers {\n' +
                '            display: flex;\n' +
                '            height: calc(100vh - 180px);\n' +
                '        }\n' +
                '        .container {\n' +
                '            width: 50%;\n' +
                '            padding: 5px;\n' +
                '            display: flex;\n' +
                '            flex-direction: column;\n' +
                '            height: 100%;\n' +
                '            box-sizing: border-box;\n' +
                '        }\n' +
                '        .pane, .inputPane {\n' +
                '            margin-top: 5px;\n' +
                '            padding: 0.6em;\n' +
                '            border: 1px solid #ccc;\n' +
                '            overflow: auto;\n' +
                '            flex-grow: 1;\n' +
                '            flex-shrink: 1;\n' +
                '            flex:1;'+
                '        }\n' +
                '        .btns{\n' +
                '            display: flex;\n' +
                '        }\n' +
                '        .btns input{\n' +
                '            margin-right: 20px;\n' +
                '        }\n' +
                '        .img_content ul{\n' +
                '            list-style: none;\n' +
                '            margin: 10px 20px;\n' +
                '            padding: 0;\n' +
                '        }\n' +
                '        .img_content li{\n' +
                '            display: flex;\n' +
                '            align-items: center;\n' +
                '            padding: 10px 0;\n' +
                '            border-bottom: 1px solid #efefef;\n' +
                '        }\n' +
                '        .img_content li img{\n' +
                '            width: 150px;\n' +
                '            margin-right: 20px;\n' +
                '        }' +
                '    </style>' +
                '<div class="containers" id="containers'+that.sourceData.oid+'">\n' +
                '        <div class="container" style="width: 40%">\n' +
                '            <div class="btns">\n' +
                '                <input type="button" id="clear'+that.sourceData.oid+'" oid="'+that.sourceData.oid+'" value="清除" style="width: 80px;height: 25px"/>\n' +
                '                <input type="button" id="save'+that.sourceData.oid+'" oid="'+that.sourceData.oid+'" value="保存" style="width: 80px;height: 25px"/>\n' +
                '                <input type="button" id="btnimages'+that.sourceData.oid+'" oid="'+that.sourceData.oid+'" value="已上传图片" style="width: 80px;height: 25px"/>\n' +
                '                <input type="button" id="uploadmdimg'+that.sourceData.oid+'" name="uploader-input" oid="'+that.sourceData.oid+'" class="uploader-file" value="上传图片" style="width: 80px;height: 25px">\n' +
                '            </div>\n' +
                '            <textarea  id="markedSource'+that.sourceData.oid+'" class="inputPane" oid="'+that.sourceData.oid+'"></textarea>\n' +
                '        </div>\n' +
                '        <div class="container" style="width: 60%">\n' +
                '            <select id="outputType'+that.sourceData.oid+'" oid="'+that.sourceData.oid+'" style="width: 150px;height: 25px;visibility: hidden">\n' +
                '                <option value="markedResult">markedResult</option>\n' +
                '                <option value="html">HTML Source</option>\n' +
                '                <option value="lexer">Lexer Data</option>\n' +
                '            </select>\n' +
                '            <div id="markedResult'+that.sourceData.oid+'" class="pane"><div class="markdownDiv"></div></div>\n' +
                '            <textarea id="html'+that.sourceData.oid+'" class="pane" readonly="readonly" style="display: none;"></textarea>\n' +
                '            <textarea id="lexer'+that.sourceData.oid+'" class="pane" readonly="readonly" style="display: none;"></textarea>\n' +
                '        </div>\n' +
                '    </div>'
        };
        this.init = function(){
            var that = this;
            webUtil.copyConfig(that,that.moduleKey);
            this.renderMD()
        };
        this.renderMD=function (){
            var that=this;
            var rendererMD = new marked.Renderer();
            marked.setOptions({
                renderer: rendererMD,
                gfm: true,
                tables: true,
                breaks: false,//允许回车换行。该选项要求 gfm 为true
                pedantic: false,
                sanitize: false,//不输出HTML
                smartLists: true,
                smartypants: false,
                highlight: function (code) {
                    return hljs.highlightAuto(code).value;
                }
            });
 
            $webUtil.ajax('get',this.url.controller+this.url.createOrGetContentByOid,{oid:this.sourceData.oid},function (result) {
                if (result.success) {
                    if (result.obj) {
                        $('#markedSource'+that.sourceData.oid).val(result.obj)
                        document.getElementById('markedResult'+that.sourceData.oid).firstChild.innerHTML = marked(result.obj);
 
                        var lexed = marked.lexer(result.obj);
                        var lexedList = that.jsonString(lexed);
                        var parsed = marked.parser(lexed);
                        document.getElementById('lexer'+that.sourceData.oid).value=lexedList
                        document.getElementById('html'+that.sourceData.oid).value=parsed
                    }
                } else {
                    webUtil.showErrorMsg(result.msg);
                }
            },function (xhr,error) {
                $webUtil.showDebugMsg(error);
            },this.backPath);
            this.bind()
            this.upload(that.sourceData.oid);
        }
 
        this.bind=function (){
            var that=this;
            // 监听输入值变化
            $('#markedSource'+that.sourceData.oid).on('keyup', function(cm){
                var oid=$(this).attr('oid')
                document.getElementById('markedResult'+oid).firstChild.innerHTML = marked(cm.currentTarget.value);
 
                var lexed = marked.lexer(cm.currentTarget.value);
                var lexedList = that.jsonString(lexed);
                var parsed = marked.parser(lexed);
                document.getElementById('lexer'+oid).value=lexedList
                document.getElementById('html'+oid).value=parsed
            })
 
            $('#clear'+that.sourceData.oid).on('click',function (e){
                var oid=$(this).attr('oid')
                document.getElementById('markedSource'+oid).value = '';
                document.getElementById('markedResult'+oid).firstChild.innerHTML= '';
            })
            $('#save'+that.sourceData.oid).on('click',function (e){
                var oid=$(this).attr('oid')
                $webUtil.ajax('post',that.url.controller+that.url.saveContentByOid,{oid:oid,content:$('#markedSource'+oid).val()},function(result){
                    if(result.success){
                        $webUtil.showMsgFromResult(result,"保存成功");
                    }else{
                        $webUtil.showErrorMsg(result.msg);
                    }
                },function(xhr,err){
                    $webUtil.showErrorMsg("请求服务出现了错误,可能服务器未开启");
                },that.backPath);
            })
            $('#outputType'+that.sourceData.oid).on('change',function (e){
                var oid=$(this).attr('oid')
                var panes=$('#containers'+oid+' .pane')
                for(var i = 0; i< panes.length; i++) {
                    if (panes[i].id === e.currentTarget.value+oid) {
                        panes[i].style.display = '';
                    } else {
                        panes[i].style.display = 'none';
                    }
                }
            })
 
            //显示已上传图片
            $('#btnimages'+that.sourceData.oid).on('click',function (){
                var oid=$(this).attr('oid')
                var imglayer=layer.open({
                    title:'已上传图片',
                    type: 1,
                    skin: 'layui-layer-rim', //加上边框
                    area: ['1200px', '600px'], //宽高
                    content: '<div class="img_content" id="img_content'+oid+'"><ul></ul><input type="text" style="opacity: 0;width: 1px" id="imgCopy'+oid+'" value="" /></div>',
                    success: function (layero) {
                        $webUtil.ajax('get',that.url.controller+that.url.gridImageInfo,{'conditionMap["ownbizOid"]':oid},function (result) {
                            if (result.success) {
                                if (result.data && result.data.length>0) {
                                    var list=[]
                                    var imgJson=[]
                                    $.each(result.data,function (i,item){
                                        item.url=that.backPath+item.url;
                                        list.push('<li>' +
                                                '<img src="'+item.url+'" alt=""/>' +
                                                '<div>' +item.url+
                                                    '<button class="btn btn-img-copy" imgsrc="'+item.url+'" style="margin-left: 20px">复制图片地址</button>' +
                                                    '<button class="btn btn-imgDiv-copy" imgsrc="'+item.url+'" style="margin-left: 20px">复制img标签</button>' +
                                                '</div>' +
                                            '</li>');
                                        imgJson.push({
                                            "alt": item.url,
                                            "pid": item.oid, //图片id
                                            "src": item.url, //原图地址
                                            "thumb":item.url //缩略图地址
                                        })
                                    })
                                    $('#img_content'+oid+' ul').append(list)
                                    $('#img_content'+oid+' img').on('click',function (){
                                        layer.photos({
                                            photos: {
                                                "title": "", //相册标题
                                                "id": 123, //相册id
                                                "start": 0, //初始显示的图片序号,默认0
                                                "data":imgJson
                                            }
                                            ,shift:5
                                        });
                                    })
                                    //复制按钮
                                    $('#img_content'+oid+' .btn-img-copy').on('click', function() {
                                        var input = document.getElementById('imgCopy'+oid);
                                        input.value='![avatar]('+$(this).attr('imgsrc')+')'
                                        $('#markedSource'+oid).val($('#markedSource'+oid).val()+input.value);
                                        document.getElementById('markedResult'+oid).firstChild.innerHTML = marked( $('#markedSource'+oid).val());
                                        // 选中文本
                                        input.select();
                                        // 执行浏览器复制命令
                                        document.execCommand("copy");
                                        //alert("成功复制到剪切版!");
                                        layer.close(imglayer)
                                    })
                                    //复制div按钮
                                    $('#img_content'+oid+' .btn-imgDiv-copy').on('click', function() {
                                        var input = document.getElementById('imgCopy'+oid);
                                        input.value='<div style="text-align:center;margin:10px 0"><img src="'+$(this).attr('imgsrc')+'"/></div>'
                                        $('#markedSource'+oid).val($('#markedSource'+oid).val()+input.value);
                                        document.getElementById('markedResult'+oid).firstChild.innerHTML = marked( $('#markedSource'+oid).val());
                                        // 选中文本
                                        input.select();
                                        // 执行浏览器复制命令
                                        document.execCommand("copy");
                                        //alert("成功复制到剪切版!");
                                        layer.close(imglayer)
                                    })
                                }
                            } else {
                                webUtil.showErrorMsg(result.msg);
                            }
                        },function (xhr,error) {
                            $webUtil.showDebugMsg(error);
                        },that.backPath);
 
 
 
                    }
                });
            })
 
        }
 
        this.upload=function (oid){
            var that=this;
            //文件上传
            layui.upload.render({
                elem: '#uploadmdimg'+oid
                ,url: that.backPath+that.url.controller+that.url.uploadPic
                ,data: {ownbizOid:oid}
                ,acceptMime: 'image/*'
                ,done: function(result, index, upload){ //上传后的回调
                    if(result.success) {
                        $webUtil.showMsgFromResult(result, "图片上传成功");
                    }
                }
                ,accept: 'images'
            })
        }
 
        this.stringRepeat=function (char, times) {
            var s = '';
            for (var i = 0; i < times; i++) {
                s += char;
            }
            return s;
        }
        this.jsonString=function (input, level) {
            level = level || 0;
            if (Array.isArray(input)) {
                if (input.length === 0) {
                    return '[]';
                }
                var items = [],
                    i;
                if (!Array.isArray(input[0]) && typeof input[0] === 'object' && input[0] !== null) {
                    for (i = 0; i < input.length; i++) {
                        items.push(this.stringRepeat(' ', 2 * level) + this.jsonString(input[i], level + 1));
                    }
                    return '[\n' + items.join('\n') + '\n]';
                }
                for (i = 0; i < input.length; i++) {
                    items.push(this.jsonString(input[i], level));
                }
                return '[' + items.join(', ') + ']';
            } else if (typeof input === 'object' && input !== null) {
                var props = [];
                for (var prop in input) {
                    props.push(prop + ':' + this.jsonString(input[prop], level));
                }
                return '{' + props.join(', ') + '}';
            } else {
                return JSON.stringify(input);
            }
        }
    };
    var cs = new Class();
    exports(cs.MODELNAME,cs);
});