src/lay/modules/layedit.js
function(iframeWin, editor, set){
    var iframeDOM = iframeWin.document
    ,body = $(iframeDOM.body)
    ,toolEvent = {
      //超链接
      link: function(range){
        var container = getContainer(range)
        ,parentNode = $(container).parent();
        
        link.call(body, {
          href: parentNode.attr('href')
          ,target: parentNode.attr('target')
        }, function(field){
          var parent = parentNode[0];
          if(parent.tagName === 'A'){
            parent.href = field.url;
          } else {
            insertInline.call(iframeWin, 'a', {
              target: field.target
              ,href: field.url
              ,text: field.url
            }, range);
          }
        });
      }
      //清除超链接
      ,unlink: function(range){
        iframeDOM.execCommand('unlink');
      }
      //表情
      ,face: function(range){
        face.call(this, function(img){
          insertInline.call(iframeWin, 'img', {
            src: img.src
            ,alt: img.alt
          }, range);
        });
      }
      //图片
      ,image: function(range){
        var that = this;
        layui.use('upload', function(upload){
          var uploadImage = set.uploadImage || {};
          upload.render({
            url: uploadImage.url
            ,method: uploadImage.type
            ,elem: $(that).find('input')[0]
            ,done: function(res){
              if(res.code == 0){
                res.data = res.data || {};
                insertInline.call(iframeWin, 'img', {
                  src: res.data.src
                  ,alt: res.data.title
                }, range);
              } else {
                layer.msg(res.msg||'上传失败');
              }
            }
          });
        });
      }
      //插入代码
      ,code: function(range){
        code.call(body, function(pre){
          insertInline.call(iframeWin, 'pre', {
            text: pre.code
            ,'lay-lang': pre.lang
          }, range);
        });
      }
      //帮助
      ,help: function(){
        layer.open({
          type: 2
          ,title: '帮助'
          ,area: ['600px', '380px']
          ,shadeClose: true
          ,shade: 0.1
          ,skin: 'layui-layer-msg'
          ,content: ['http://www.layui.com/about/layedit/help.html', 'no']
        });
      }
    }
    ,tools = editor.find('.layui-layedit-tool')
    
    ,click = function(){
      var othis = $(this)
      ,events = othis.attr('layedit-event')
      ,command = othis.attr('lay-command');
      
      if(othis.hasClass(ABLED)) return;

      body.focus();
      
      var range = Range(iframeDOM)
      ,container = range.commonAncestorContainer
      
      if(command){
        iframeDOM.execCommand(command);
        if(/justifyLeft|justifyCenter|justifyRight/.test(command)){
          iframeDOM.execCommand('formatBlock', false, '<p>');
        }
        setTimeout(function(){
          body.focus();
        }, 10);
      } else {
        toolEvent[events] && toolEvent[events].call(this, range);
      }
      toolCheck.call(iframeWin, tools, othis);
    }
    
    ,isClick = /image/

    tools.find('>i').on('mousedown', function(){
      var othis = $(this)
      ,events = othis.attr('layedit-event');
      if(isClick.test(events)) return;
      click.call(this)
    }).on('click', function(){
      var othis = $(this)
      ,events = othis.attr('layedit-event');
      if(!isClick.test(events)) return;
      click.call(this)
    });
    
    //触发内容区域
    body.on('click', function(){
      toolCheck.call(iframeWin, tools);
      layer.close(face.index);
    });
  }
function(iframeWin, editor, set){
    var iframeDOM = iframeWin.document
    ,body = $(iframeDOM.body)
    ,toolEvent = {
      //超链接
      link: function(range){
        var container = getContainer(range)
        ,parentNode = $(container).parent();
        
        link.call(body, {
          href: parentNode.attr('href')
          ,target: parentNode.attr('target')
        }, function(field){
          var parent = parentNode[0];
          if(parent.tagName === 'A'){
            parent.href = field.url;
          } else {
            insertInline.call(iframeWin, 'a', {
              target: field.target
              ,href: field.url
              ,text: field.url
            }, range);
          }
        });
      }
      //清除超链接
      ,unlink: function(range){
        iframeDOM.execCommand('unlink');
      }
      //表情
      ,face: function(range){
        face.call(this, function(img){
          insertInline.call(iframeWin, 'img', {
            src: img.src
            ,alt: img.alt
          }, range);
        });
      }
      //图片
      ,image: function(range){
        var that = this;
        layui.use('upload', function(upload){
          var uploadImage = set.uploadImage || {};
          upload.render({
            url: uploadImage.url
            ,method: uploadImage.type
            ,elem: $(that).find('input')[0]
            ,done: function(res){
              if(res.code == 0){
                res.data = res.data || {};
                insertInline.call(iframeWin, 'img', {
                  src: res.data.src
                  ,alt: res.data.title
                }, range);
              } else {
                layer.msg(res.msg||'上传失败');
              }
            }
          });
        });
      }
      //插入代码
      ,code: function(range){
        code.call(body, function(pre){
          insertInline.call(iframeWin, 'pre', {
            text: pre.code
            ,'lay-lang': pre.lang
          }, range);
        });
      }
      //帮助
      ,help: function(){
        layer.open({
          type: 2
          ,title: '帮助'
          ,area: ['600px', '380px']
          ,shadeClose: true
          ,shade: 0.1
          ,skin: 'layui-layer-msg'
          ,content: ['http://www.layui.com/about/layedit/help.html', 'no']
        });
      }
    }
    ,tools = editor.find('.layui-layedit-tool')
    
    ,click = function(){
      var othis = $(this)
      ,events = othis.attr('layedit-event')
      ,command = othis.attr('lay-command');
      
      if(othis.hasClass(ABLED)) return;

      body.focus();
      
      var range = Range(iframeDOM)
      ,container = range.commonAncestorContainer
      
      if(command){
        iframeDOM.execCommand(command);
        if(/justifyLeft|justifyCenter|justifyRight/.test(command)){
          iframeDOM.execCommand('formatBlock', false, '<p>');
        }
        setTimeout(function(){
          body.focus();
        }, 10);
      } else {
        toolEvent[events] && toolEvent[events].call(this, range);
      }
      toolCheck.call(iframeWin, tools, othis);
    }
    
    ,isClick = /image/

    tools.find('>i').on('mousedown', function(){
      var othis = $(this)
      ,events = othis.attr('layedit-event');
      if(isClick.test(events)) return;
      click.call(this)
    }).on('click', function(){
      var othis = $(this)
      ,events = othis.attr('layedit-event');
      if(!isClick.test(events)) return;
      click.call(this)
    });
    
    //触发内容区域
    body.on('click', function(){
      toolCheck.call(iframeWin, tools);
      layer.close(face.index);
    });
  }
function ( iframeWin , editor , set ) { var iframeDOM = iframeWin . document , body = $ ( iframeDOM . body ) , toolEvent = { //超链接 link : function ( range ) { var container = getContainer ( range ) , parentNode = $ ( container ) . parent ( ) ; link . call ( body , { href : parentNode . attr ( 'href' ) , target : parentNode . attr ( 'target' ) } , function ( field ) { var parent = parentNode [ 0 ] ; if ( parent . tagName === 'A' ) { parent . href = field . url ; } else { insertInline . call ( iframeWin , 'a' , { target : field . target , href : field . url , text : field . url } , range ) ; } } ) ; } //清除超链接 , unlink : function ( range ) { iframeDOM . execCommand ( 'unlink' ) ; } //表情 , face : function ( range ) { face . call ( this , function ( img ) { insertInline . call ( iframeWin , 'img' , { src : img . src , alt : img . alt } , range ) ; } ) ; } //图片 , image : function ( range ) { var that = this ; layui . use ( 'upload' , function ( upload ) { var uploadImage = set . uploadImage || { } ; upload . render ( { url : uploadImage . url , method : uploadImage . type , elem : $ ( that ) . find ( 'input' ) [ 0 ] , done : function ( res ) { if ( res . code == 0 ) { res . data = res . data || { } ; insertInline . call ( iframeWin , 'img' , { src : res . data . src , alt : res . data . title } , range ) ; } else { layer . msg ( res . msg || '上传失败'); } } } ) ; } ) ; } //插入代码 , code : function ( range ) { code . call ( body , function ( pre ) { insertInline . call ( iframeWin , 'pre' , { text : pre . code , 'lay-lang' : pre . lang } , range ) ; } ) ; } //帮助 , help : function ( ) { layer . open ( { type : 2 , title : '帮助' , area : [ '600px' , '380px' ] , shadeClose : true , shade : 0.1 , skin : 'layui-layer-msg' , content : [ 'http://www.layui.com/about/layedit/help.html' , 'no' ] } ) ; } } , tools = editor . find ( '.layui-layedit-tool' ) , click = function ( ) { var othis = $ ( this ) , events = othis . attr ( 'layedit-event' ) , command = othis . attr ( 'lay-command' ) ; if ( othis . hasClass ( ABLED ) ) return ; body . focus ( ) ; var range = Range ( iframeDOM ) , container = range . commonAncestorContainer if ( command ) { iframeDOM . execCommand ( command ) ; if ( / justifyLeft|justifyCenter|justifyRight / . test ( command ) ) { iframeDOM . execCommand ( 'formatBlock' , false , '<p>' ) ; } setTimeout ( function ( ) { body . focus ( ) ; } , 10 ) ; } else { toolEvent [ events ] && toolEvent [ events ] . call ( this , range ) ; } toolCheck . call ( iframeWin , tools , othis ) ; } , isClick = / image / tools . find ( '>i' ) . on ( 'mousedown' , function ( ) { var othis = $ ( this ) , events = othis . attr ( 'layedit-event' ) ; if ( isClick . test ( events ) ) return ; click . call ( this ) } ) . on ( 'click' , function ( ) { var othis = $ ( this ) , events = othis . attr ( 'layedit-event' ) ; if ( ! isClick . test ( events ) ) return ; click . call ( this ) } ) ; //触发内容区域 body . on ( 'click' , function ( ) { toolCheck . call ( iframeWin , tools ) ; layer . close ( face . index ) ; } ) ; }
iframe初始化
iframe初始化