在后台撰写文章增加一个批量插入附件的按钮
This commit is contained in:
浪子 2025-04-07 10:55:57 +08:00
parent 4dce3365b6
commit a6883c5ce7
1 changed files with 56 additions and 0 deletions

View File

@ -449,4 +449,60 @@ function commentApprove($widget, $email = NULL)
$result['commentNum'] = $commentNum;
}
return $result;
}
/**
* 修改附件插入功能
*/
// 添加批量插入按钮的JavaScript
Typecho_Plugin::factory('admin/write-post.php')->bottom = array('MyHelper', 'addBatchInsertButton');
Typecho_Plugin::factory('admin/write-page.php')->bottom = array('MyHelper', 'addBatchInsertButton');
class MyHelper {
public static function addBatchInsertButton() {
?>
<script>
$(document).ready(function() {
// 添加批量插入按钮
var batchButton = $('<button type="button" class="btn primary" id="batch-insert">批量插入所有附件</button>');
$('#file-list').before(batchButton);
// 修改单个附件的插入格式
Typecho.insertFileToEditor = function(title, url, isImage) {
var textarea = $('#text'), sel = textarea.getSelection(),
insertContent = isImage ? '![' + title + '](' + url + ')' :
'[' + title + '](' + url + ')';
textarea.replaceSelection(insertContent);
textarea.focus();
};
// 批量插入功能
$('#batch-insert').click(function() {
var content = '';
$('#file-list li').each(function() {
var $this = $(this),
title = $this.find('.insert').text(),
url = $this.data('url'),
isImage = $this.data('image') == 1;
content += isImage ? '![' + title + '](' + url + ')\n' :
'[' + title + '](' + url + ')\n';
});
var textarea = $('#text');
var pos = textarea.getSelection();
var newContent = textarea.val();
if (pos.start === pos.end) {
newContent = newContent.substring(0, pos.start) + content + newContent.substring(pos.start);
} else {
newContent = newContent.substring(0, pos.start) + content + newContent.substring(pos.end);
}
textarea.val(newContent);
textarea.focus();
});
});
</script>
<?php
}
}