| 1 |
/** |
| 2 |
* CodeMirror Media Insertion functionality. |
| 3 |
* |
| 4 |
* Handles media library integration for CodeMirror editor instances. |
| 5 |
* |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
jQuery(document).ready(function ($) { |
| 9 |
|
| 10 |
/** |
| 11 |
* Insert string into CodeMirror editor. |
| 12 |
* |
| 13 |
* @since 1.0.0 |
| 14 |
* |
| 15 |
* @param {Object} editor CodeMirror instance. |
| 16 |
* @param {string} str String to insert. |
| 17 |
*/ |
| 18 |
function insertString(editor, str) { |
| 19 |
var selection = editor.getSelection(); |
| 20 |
|
| 21 |
if (selection.length > 0) { |
| 22 |
editor.replaceSelection(str); |
| 23 |
} else { |
| 24 |
var doc = editor.getDoc(); |
| 25 |
var cursor = doc.getCursor(); |
| 26 |
var pos = { |
| 27 |
line: cursor.line, |
| 28 |
ch: cursor.ch |
| 29 |
}; |
| 30 |
|
| 31 |
doc.replaceRange(str, pos); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Generate attachment HTML via AJAX. |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
* |
| 40 |
* @param {Object} props Display properties. |
| 41 |
* @param {Object} attachment Attachment object. |
| 42 |
* @return {Promise} jQuery AJAX promise. |
| 43 |
*/ |
| 44 |
function attachmentHtml(props, attachment) { |
| 45 |
var caption = attachment.caption; |
| 46 |
var options; |
| 47 |
var html; |
| 48 |
|
| 49 |
// Clear caption if disabled globally. |
| 50 |
if (!wp.media.view.settings.captions) { |
| 51 |
delete attachment.caption; |
| 52 |
} |
| 53 |
|
| 54 |
props = wp.media.string.props(props, attachment); |
| 55 |
|
| 56 |
options = { |
| 57 |
id: attachment.id, |
| 58 |
post_content: attachment.description, |
| 59 |
post_excerpt: caption |
| 60 |
}; |
| 61 |
|
| 62 |
if (props.linkUrl) { |
| 63 |
options.url = props.linkUrl; |
| 64 |
} |
| 65 |
|
| 66 |
if ('image' === attachment.type) { |
| 67 |
html = wp.media.string.image(props); |
| 68 |
|
| 69 |
_.each({ |
| 70 |
align: 'align', |
| 71 |
size: 'image-size', |
| 72 |
alt: 'image_alt' |
| 73 |
}, function (option, prop) { |
| 74 |
if (props[prop]) { |
| 75 |
options[option] = props[prop]; |
| 76 |
} |
| 77 |
}); |
| 78 |
} else if ('video' === attachment.type) { |
| 79 |
html = wp.media.string.video(props, attachment); |
| 80 |
} else if ('audio' === attachment.type) { |
| 81 |
html = wp.media.string.audio(props, attachment); |
| 82 |
} else { |
| 83 |
html = wp.media.string.link(props); |
| 84 |
options.post_title = props.title; |
| 85 |
} |
| 86 |
|
| 87 |
return $.ajax({ |
| 88 |
type: 'POST', |
| 89 |
dataType: 'json', |
| 90 |
url: ajaxurl, |
| 91 |
data: { |
| 92 |
action: 'send-attachment-to-editor', |
| 93 |
nonce: wp.media.view.settings.nonce.sendToEditor, |
| 94 |
attachment: options, |
| 95 |
html: html, |
| 96 |
post_id: wp.media.view.settings.post.id |
| 97 |
} |
| 98 |
}); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Process media selection and insert into editor. |
| 103 |
* |
| 104 |
* @since 1.0.0 |
| 105 |
* |
| 106 |
* @param {Object} editor CodeMirror instance. |
| 107 |
* @param {Object} fileFrame Media frame instance. |
| 108 |
*/ |
| 109 |
function processMediaSelection(editor, fileFrame) { |
| 110 |
var selection = fileFrame.state().get('selection'); |
| 111 |
var promises = []; |
| 112 |
|
| 113 |
selection.each(function (attachment) { |
| 114 |
var props = fileFrame.state().display(attachment).toJSON(); |
| 115 |
var promise = attachmentHtml(props, attachment.toJSON()).done(function (response) { |
| 116 |
insertString(editor, response.data); |
| 117 |
}); |
| 118 |
|
| 119 |
promises.push(promise); |
| 120 |
}); |
| 121 |
|
| 122 |
$.when.apply($, promises).always(function () { |
| 123 |
fileFrame.close(); |
| 124 |
}); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Handle media insertion button clicks. |
| 129 |
* |
| 130 |
* @since 1.0.0 |
| 131 |
*/ |
| 132 |
$('.insert-codemirror-media').on('click', function (event) { |
| 133 |
event.preventDefault(); |
| 134 |
event.stopImmediatePropagation(); |
| 135 |
event.stopPropagation(); |
| 136 |
$(this).removeClass('add_media'); |
| 137 |
|
| 138 |
var editor = $('#wp-content-editor-container .CodeMirror')[0].CodeMirror; |
| 139 |
var fileFrame; |
| 140 |
|
| 141 |
fileFrame = wp.media.frames.file_frame = wp.media({ |
| 142 |
frame: 'post', |
| 143 |
state: 'insert', |
| 144 |
multiple: false |
| 145 |
}); |
| 146 |
|
| 147 |
// Remove old handlers and add insert handler only. |
| 148 |
fileFrame.off('insert').on('insert', function () { |
| 149 |
processMediaSelection(editor, fileFrame); |
| 150 |
}); |
| 151 |
|
| 152 |
fileFrame.open(); |
| 153 |
}); |
| 154 |
|
| 155 |
}); |