-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.php
executable file
·117 lines (100 loc) · 2.81 KB
/
Plugin.php
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
<?php
/**
* 文章编辑页面标签选择器插件
*
* @package TagSelector
* @author Yuanzhumc
* @version 1.0.0
* @link https://blog.berfen.com/76.html
*/
class TagSelector_Plugin implements Typecho_Plugin_Interface
{
public static function activate()
{
Typecho_Plugin::factory('admin/write-post.php')->bottom = array('TagSelector_Plugin', 'render');
}
public static function deactivate()
{
}
public static function config(Typecho_Widget_Helper_Form $form)
{
}
public static function personalConfig(Typecho_Widget_Helper_Form $form)
{
}
public static function render()
{
$db = Typecho_Db::get();
$tags = $db->fetchAll($db->select()->from('table.metas')->where('type = ?', 'tag'));
$tagsArray = [];
foreach ($tags as $tag) {
$tagsArray[] = $tag['name'];
}
$tagsJson = json_encode($tagsArray);
echo <<<HTML
<style>
.tag-selector {
margin: 10px 0;
padding: 5px;
border: 1px solid #ccc;
max-height: 150px;
overflow-y: auto;
display: flex;
flex-wrap: wrap;
}
.tag-selector span {
background-color: #f3f3f3;
padding: 3px 8px;
margin: 2px;
border-radius: 3px;
cursor: pointer;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function () {
let tagsArray = {$tagsJson};
let tagsInput = document.getElementById('tags');
let tokenInput = document.getElementById('token-input-tags');
function addTag(tag) {
if (tokenInput) {
tokenInput.value = tag;
const keyboardEvent = new KeyboardEvent('keydown', {
key: 'Enter',
code: 'Enter',
keyCode: 13,
which: 13,
});
tokenInput.dispatchEvent(keyboardEvent);
tagsArray = tagsArray.filter(t => t !== tag);
renderTags();
}
}
function renderTags() {
let tagsHTML = '';
tagsArray.forEach(tag => {
tagsHTML += '<span data-tag="'+tag+'">'+tag+'</span>';
});
tagSelector.innerHTML = tagsHTML;
tagSelector.querySelectorAll('span').forEach(span => {
span.addEventListener('click', function () {
addTag(this.dataset.tag);
});
});
}
const tagSelector = document.createElement('div');
tagSelector.classList.add('tag-selector');
const tagsElement = document.getElementById('advance-panel');
if (tagsElement) {
tagsElement.parentNode.insertBefore(tagSelector, tagsElement.nextSibling);
}
// 将编辑页面中已有的标签从标签选择框中删除
if (tagsInput) {
const inputTags = tagsInput.value.split(',').map(t => t.trim()).filter(t => t.length > 0);
tagsArray = tagsArray.filter(tag => !inputTags.includes(tag));
}
renderTags();
});
</script>
HTML;
}
}