typecho给主题自定义字段添加智能输入

弄主题的时候,有时候自定义字段需要填写相对复杂的格式内容,才能便于页面上的输出

例如

QQ图片20201019080009.png

如果按格式输出,编辑内容格式去填,有可能对一些用户来说,比较抽象复制

但是如果这样的话,可能就相对来说人性化一点

QQ图片20201019080142.png

制作教程

  1. /**
  2. * 后台字段处理
  3. */
  4. Typecho_Plugin::factory('admin/write-post.php')->bottom = array('myyodu', 'one');
  5. class myyodu {
  6. public static function one()
  7. {
  8. ?>
  9. <script src="/usr/themes/spzhi/assets/wmd.js"></script>
  10. <link rel="stylesheet" href="/usr/themes/spzhi/assets/setting.fb.css">
  11. <?php
  12. }}

以上是添加到functions.php的相关代码,意思是处理发布页面的时候,添加相对应的js和css样式

css样式这里就多说了,就是简单美化一下背景和阴影部分,不添加也可以的,就和typecho的默认弹窗一样

这里说的是js代码,涉及到添加窗口,隐藏窗口,获取填入的内容信息并且反馈到指定的id区域里面

不便一一说明,下面发js全部的代码复制出来

  1. /**
  2. * @description typecho后台编辑器插入的js
  3. * @author vv
  4. * @version 0
  5. */
  6. window.onload = function () {
  7. /* 样式栏 */
  8. $(document).ready(function(){
  9. if ($("#custom-field").length >0){
  10. $('.origin').after('<a href="javascript:;" class="origin_btn wmd-post-button">内容生成</a>');
  11. $('.playnum').after('<a href="javascript:;" class="origin_btn wmd-play-button">视频生成</a>');
  12. $(document).on('click', '.wmd-post-button', function() {
  13. $('body').append(
  14. '<div id="postPanel">'+
  15. '<div class="wmd-prompt-background" style="position: fixed; top: 0px; z-index: 1000; opacity: 0.5; height: 100%; left: 0px; width: 100%;"></div>'+
  16. '<div class="wmd-prompt-dialog">'+
  17. '<div>'+
  18. '<p><b>扩展资料</b></p><p>请按格式填入指定内容,以免生成的时候出错。</p>'+
  19. '<p><labe>输入文章标题</labe></p><input type="text" name="ucgtit" placeholder="必填"></input>'+
  20. '<p><labe>输入文章内容</labe></p><textarea type="text" name="ucgcon" placeholder="必填"></textarea>'+
  21. '</div>'+
  22. '<form>'+
  23. '<button type="button" class="btn btn-s primary" id="post_ok">确定</button>'+
  24. '<button type="button" class="btn btn-s" id="post_cancel">取消</button>'+
  25. '</form>'+
  26. '</div>'+
  27. '</div>');
  28. });
  29. $(document).on('click', '.wmd-play-button', function() {
  30. $('body').append(
  31. '<div id="postPanel">'+
  32. '<div class="wmd-prompt-background" style="position: fixed; top: 0px; z-index: 1000; opacity: 0.5; height: 100%; left: 0px; width: 100%;"></div>'+
  33. '<div class="wmd-prompt-dialog">'+
  34. '<div>'+
  35. '<p><b>输入视频</b></p><p>请按格式填入指定内容,以免生成的时候出错。</p>'+
  36. '<p><labe>免费/付费</labe></p><input type="text" name="playfei" placeholder="必填"></input>'+
  37. '<p><labe>视频标题</labe></p><input type="text" name="playtit" placeholder="必填"></input>'+
  38. '<p><labe>视频链接</labe></p><input type="text" name="playrurl" placeholder="必填"></input>'+
  39. '<p><labe>视频时间长度</labe></p><input type="text" name="playtime" placeholder="必填"></input>'+
  40. '</div>'+
  41. '<form>'+
  42. '<button type="button" class="btn btn-s primary" id="play_ok">确定</button>'+
  43. '<button type="button" class="btn btn-s" id="post_cancel">取消</button>'+
  44. '</form>'+
  45. '</div>'+
  46. '</div>');
  47. });
  48. /* 执行区 *///否定
  49. $(document).on('click','#post_cancel',function() {
  50. $('#postPanel').remove();
  51. $('textarea').focus();
  52. });
  53. $(document).on('click', '#post_ok',function () {
  54. var ucgtit = '' + $('.wmd-prompt-dialog input[name = "ucgtit"]').val() + '';
  55. var ucgcon = '' + $('.wmd-prompt-dialog textarea[name = "ucgcon"]').val() + '';
  56. var textContent = ucgtit + '||'+ ucgcon + '\r\n';
  57. myField = document.getElementsByClassName('origin');
  58. myField = myField[0];
  59. inserContentToTextArea(myField,textContent,'#postPanel');
  60. })
  61. $(document).on('click', '#play_ok',function () {
  62. var playfei = '' + $('.wmd-prompt-dialog input[name = "playfei"]').val() + '';
  63. var playtit = '' + $('.wmd-prompt-dialog input[name = "playtit"]').val() + '';
  64. var playrurl = '' + $('.wmd-prompt-dialog input[name = "playrurl"]').val() + '';
  65. var playtime = '' + $('.wmd-prompt-dialog input[name = "playtime"]').val() + '';
  66. var textContent = playfei + '||'+ playtit + '||'+ playrurl + '||'+ playtime + '\r\n';
  67. myField = document.getElementsByClassName('playnum');
  68. myField = myField[0];
  69. inserContentToTextArea(myField,textContent,'#postPanel');
  70. })
  71. }
  72. });
  73. };
  74. function inserContentToTextArea(myField,textContent,modelId) {
  75. $(modelId).remove();
  76. if (document.selection) {//IE浏览器
  77. myField.focus();
  78. var sel = document.selection.createRange();
  79. sel.text = textContent;
  80. myField.focus();
  81. } else if (myField.selectionStart || myField.selectionStart == '0') {
  82. //FireFox、Chrome
  83. var startPos = myField.selectionStart;
  84. var endPos = myField.selectionEnd;
  85. var cursorPos = startPos;
  86. myField.value = myField.value.substring(0, startPos)
  87. + textContent
  88. + myField.value.substring(endPos, myField.value.length);
  89. cursorPos += textContent.length;
  90. myField.selectionStart = cursorPos;
  91. myField.selectionEnd = cursorPos;
  92. myField.focus();
  93. }
  94. else{//其他环境
  95. myField.value += textContent;
  96. myField.focus();
  97. }
  98. }

如图所示:

QQ图片20201019081109.png

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/670
1 条评论
3.9k

发表评论

仅有一条评论

  1. 谢辞.     Win 10 /    QQ浏览器
    2022-06-17 14:10

    把自定义字段themeFields也发出来呗!!!
    我这边没有显示出按钮,头大!!!

!