Skip to content

Commit

Permalink
Merge pull request #10 from 10up/feature/add-code-mirror
Browse files Browse the repository at this point in the history
Add CodeMirror with line numbers and basic highlighting. Linting yet to come.
  • Loading branch information
helen authored Dec 15, 2017
2 parents f139722 + ba6230f commit 58bb20a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
25 changes: 24 additions & 1 deletion inc/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function admin_enqueue_scripts( $hook ) {
return;
}

wp_enqueue_script( 'adstxt', plugins_url( '/js/admin.js', dirname( __FILE__ ) ), array( 'jquery', 'wp-backbone' ), false, true );
wp_enqueue_script( 'adstxt', plugins_url( '/js/admin.js', dirname( __FILE__ ) ), array( 'jquery', 'wp-backbone', 'wp-codemirror' ), false, true );
wp_enqueue_style( 'code-editor' );

$strings = array(
'saved' => __( 'Ads.txt saved', 'ads-txt' ),
Expand All @@ -26,6 +27,28 @@ function admin_enqueue_scripts( $hook ) {
}
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\admin_enqueue_scripts' );

/**
* Output some CSS directly in the head of the document.
*
* Should there ever be more than ~25 lines of CSS, this should become a separate file.
*
* @return void
*/
function admin_head_css() {
?>
<style>
.CodeMirror {
width: 100%;
min-height: 60vh;
height: calc( 100vh - 295px );
border: 1px solid #ddd;
box-sizing: border-box;
}
</style>
<?php
}
add_action( 'admin_head-settings_page_adstxt-settings', __NAMESPACE__ . '\admin_head_css' );

/**
* Add admin menu page.
*
Expand Down
14 changes: 11 additions & 3 deletions js/admin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
( function( $, _ ) {
var submit = $( document.getElementById( 'submit' ) ),
notificationArea = $( document.getElementById( 'adstxt-notification-area' ) ),
notificationTemplate = wp.template( 'adstext-notice' );
notificationArea = $( document.getElementById( 'adstxt-notification-area' ) ),
notificationTemplate = wp.template( 'adstext-notice' ),
editor = wp.CodeMirror.fromTextArea( document.getElementById( 'adstxt_content' ), {
lineNumbers: true,
mode: 'shell'
} );

submit.on( 'click', function( e ){
e.preventDefault();
Expand All @@ -17,6 +21,9 @@
// clear any existing messages
notices.remove();

// Copy the code mirror contents into for for submission.
textarea.val( editor.getValue() );

$.ajax({
type: 'POST',
dataType: 'json',
Expand Down Expand Up @@ -53,5 +60,6 @@
} else {
submit.attr( 'disabled', 'disabled' );
}
})
} );

} )( jQuery, _ );
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: helen, adamsilverstein, 10up
Author URI: http://10up.com
Plugin URI: https://github.com/10up/ads-txt
Tags: ads.txt, ads, ad manager, advertising, publishing, publishers
Requires at least: 4.6
Requires at least: 4.9
Tested up to: 4.9.1
Requires PHP: 5.3
Stable tag: trunk
Expand Down

0 comments on commit 58bb20a

Please sign in to comment.