Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/css/manage.scss
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,15 @@ td.column-description {
}
}
}

/* Drag and Drop Sorting */

.sortable-placeholder {
background-color: #f9f9f9;
border: 1px dashed #ccc;
block-size: 50px;
}

.wp-list-table tbody tr {
cursor: move;
}
3 changes: 2 additions & 1 deletion src/js/manage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { handleShowCloudPreview, handleSnippetActivationSwitches, handleSnippetPriorityChanges } from './services/manage'
import { handleShowCloudPreview, handleSnippetActivationSwitches, handleSnippetOrdering, handleSnippetPriorityChanges } from './services/manage'

handleSnippetActivationSwitches()
handleSnippetPriorityChanges()
handleShowCloudPreview()
handleSnippetOrdering()
1 change: 1 addition & 0 deletions src/js/services/manage/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { handleSnippetActivationSwitches } from './activation'
export { handleSnippetPriorityChanges } from './priority'
export { handleShowCloudPreview } from './cloud'
export { handleSnippetOrdering } from './ordering'
36 changes: 36 additions & 0 deletions src/js/services/manage/ordering.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import $ from 'jquery'
import { updateSnippet } from './requests'
import type { Snippet } from '../../types/Snippet'

export const handleSnippetOrdering = () => {
const table = $('.wp-list-table tbody')

if (!table.length) {
return
}

(<any> table).sortable({
items: '> tr',
cursor: 'move',
axis: 'y',
containment: 'parent',
cancel: 'input, textarea, button, select, option, a',
placeholder: 'sortable-placeholder',
update: () => {
const rows = table.find('tr')

rows.each(function (index) {
const row = $(this)
const priorityInput = row.find('.snippet-priority')
const currentPriority = parseInt(<string> priorityInput.val(), 10)
const newPriority = index + 1

if (currentPriority !== newPriority) {
priorityInput.val(newPriority)
const snippet: Partial<Snippet> = { priority: newPriority }
updateSnippet('priority', row[0], snippet)
}
})
}
})
}
4 changes: 2 additions & 2 deletions src/php/admin-menus/class-manage-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public function enqueue_assets() {
wp_enqueue_script(
'code-snippets-manage-js',
plugins_url( 'dist/manage.js', $plugin->file ),
[ 'wp-i18n' ],
[ 'jquery-ui-sortable', 'wp-i18n' ],
$plugin->version,
true
);
Expand Down Expand Up @@ -334,4 +334,4 @@ public function ajax_callback() {

wp_send_json_success();
}
}
}