Skip to content

Commit f23a30a

Browse files
Add drag-and-drop functionality to snippets table
1 parent 5311ee9 commit f23a30a

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

src/css/manage.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,15 @@ td.column-description {
213213
}
214214
}
215215
}
216+
217+
/* Drag and Drop Sorting */
218+
219+
.sortable-placeholder {
220+
background-color: #f9f9f9;
221+
border: 1px dashed #ccc;
222+
block-size: 50px;
223+
}
224+
225+
.wp-list-table tbody tr {
226+
cursor: move;
227+
}

src/js/manage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { handleShowCloudPreview, handleSnippetActivationSwitches, handleSnippetPriorityChanges } from './services/manage'
1+
import { handleShowCloudPreview, handleSnippetActivationSwitches, handleSnippetOrdering, handleSnippetPriorityChanges } from './services/manage'
22

33
handleSnippetActivationSwitches()
44
handleSnippetPriorityChanges()
55
handleShowCloudPreview()
6+
handleSnippetOrdering()

src/js/services/manage/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { handleSnippetActivationSwitches } from './activation'
22
export { handleSnippetPriorityChanges } from './priority'
33
export { handleShowCloudPreview } from './cloud'
4+
export { handleSnippetOrdering } from './ordering'

src/js/services/manage/ordering.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import $ from 'jquery'
2+
import { updateSnippet } from './requests'
3+
import type { Snippet } from '../../types/Snippet'
4+
5+
export const handleSnippetOrdering = () => {
6+
const table = $('.wp-list-table tbody')
7+
8+
if (!table.length) {
9+
return
10+
}
11+
12+
(<any> table).sortable({
13+
items: '> tr',
14+
cursor: 'move',
15+
axis: 'y',
16+
containment: 'parent',
17+
cancel: 'input, textarea, button, select, option, a',
18+
placeholder: 'sortable-placeholder',
19+
update: () => {
20+
const rows = table.find('tr')
21+
22+
rows.each(function (index) {
23+
const row = $(this)
24+
const priorityInput = row.find('.snippet-priority')
25+
const currentPriority = parseInt(<string> priorityInput.val(), 10)
26+
const newPriority = index + 1
27+
28+
if (currentPriority !== newPriority) {
29+
priorityInput.val(newPriority)
30+
const snippet: Partial<Snippet> = { priority: newPriority }
31+
updateSnippet('priority', row[0], snippet)
32+
}
33+
})
34+
}
35+
})
36+
}

src/php/admin-menus/class-manage-menu.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public function enqueue_assets() {
193193
wp_enqueue_script(
194194
'code-snippets-manage-js',
195195
plugins_url( 'dist/manage.js', $plugin->file ),
196-
[ 'wp-i18n' ],
196+
[ 'jquery-ui-sortable', 'wp-i18n' ],
197197
$plugin->version,
198198
true
199199
);
@@ -334,4 +334,4 @@ public function ajax_callback() {
334334

335335
wp_send_json_success();
336336
}
337-
}
337+
}

0 commit comments

Comments
 (0)