@@ -5,7 +5,17 @@ import "./css/main.scss"
55import "./css/pygments.css"
66import "./css/github-markdown.css"
77import { connect } from "react-redux" ;
8- import { bookSetState , bookState , closeMessage , movePage , moveStep , ranCode , setDeveloperMode , stepIndex } from "./book/store" ;
8+ import {
9+ addMessage ,
10+ bookSetState ,
11+ bookState ,
12+ closeMessage ,
13+ movePage ,
14+ moveStep ,
15+ ranCode ,
16+ setDeveloperMode ,
17+ stepIndex
18+ } from "./book/store" ;
919import Popup from "reactjs-popup" ;
1020import AceEditor from "react-ace" ;
1121import "ace-builds/src-noconflict/mode-python" ;
@@ -91,7 +101,8 @@ class AppComponent extends React.Component {
91101 const showBirdseye = page_index >= _ . findIndex ( pages , { slug : "IntroducingBirdseye" } ) ;
92102
93103 return < div className = "book-container" >
94- < div className = "book-text markdown-body" >
104+ < div className = "book-text markdown-body"
105+ onCopy = { checkCopy } >
95106 < h1 dangerouslySetInnerHTML = { { __html : page . title } } />
96107 { page . steps . slice ( 0 , step_index + 1 ) . map ( ( part , index ) =>
97108 < div key = { index } id = { `step-text-${ index } ` } >
@@ -313,6 +324,37 @@ const SettingsModal = ({user}) => (
313324 </ div >
314325)
315326
327+ const checkCopy = ( ) => {
328+ const selection = document . getSelection ( ) ;
329+ const codeElement = ( node ) => node . parentElement . closest ( "code" ) ;
330+ if (
331+ [ ...document . querySelectorAll ( ".book-text code" ) ]
332+ . filter ( node => selection . containsNode ( node ) )
333+ . concat ( [
334+ codeElement ( selection . anchorNode ) ,
335+ codeElement ( selection . focusNode ) ,
336+ ] )
337+ . some ( ( node ) => node && ! node . classList . contains ( "copyable" ) )
338+ ) {
339+ addMessage ( `
340+ <div>
341+ <p><strong>STOP!</strong></p>
342+ <p>
343+ Try to avoid copy pasting code. You will learn, absorb, and remember better if you
344+ type in the code yourself.
345+ </p>
346+ <p>
347+ When copying is appropriate, there will be a button to click to make it easy.
348+ If there's no button, try typing.
349+ </p>
350+ <p>
351+ Having said that, we're not going to force you. Copy if you really want to.
352+ </p>
353+ </div>
354+ ` ) ;
355+ }
356+ }
357+
316358
317359export const App = connect (
318360 state => ( {
0 commit comments