使用CK-Editor将光标指针移动到第一个位置onclick textarea

时间:2016-04-05 12:15:46

标签: javascript ckeditor ckeditor4.x

我正在使用CK-Editor 4。我在其中创建了指令。当我点击Textarea时,我的光标指针是Move to First position。当我点击Textarea中的任何一个地方时,我希望我的光标位置可用。在点击最后一个位置之前,它不能移动到第一个或最后一个位置。

vpb.directive('ckeInline', function ($timeout, $parse, $rootScope,$) {
return {
require: "ngModel",
link: function (scope, element, attrs, ngModel) {
  if (attrs.ckeInlineShowtitle) ngModel = "";
  isMobile = $(".ipad, .ipod, .android").length > 0;

  var destroy_this_editor = function(id)
  {

      setTimeout(function () {
        try{
            CKEDITOR.instances[id].destroy(true);
        } catch (ex) {
        }
      },500)

  }

  if (!isMobile) {
      var create_and_focus = function () {
          if (scope.setup.edit_mode || attrs.ckeNonEditMode) {
              attrs.$set('contenteditable', true);
              var a_id = attrs.ckeInlineId;
              var menu_selector = attrs.ckeInlineMenu;
              //get editor
              //create editor if doesn't exist
              var e = CKEDITOR.instances[a_id];
              if (!e) {
                  element.menu = $(menu_selector);
                  //set up behavior for menu
                  uif.wire_up_menu(element, scope);
                  //hide all menu bars
                  $("nav.text-editor-menu-bar").hide();
                  //create editor
                  var config = {
                      toolbar: "More",
                      on: {
                          'blur': function () {
                              save_editor_content(function () {
                                  destroy_this_editor(a_id);
                              });
                          },
                          'focusout': function () {
                              save_editor_content(function () {
                                  destroy_this_editor(a_id);
                              });
                          },
                          'focus': function () {
                              //show the current menu
                              element.menu.show();
                              //fix menu width
                              uif.stretch_menu(element);
                          },
                          'instanceReady': function (event,element) {
 //----------------I think,Here i want logic to set caret sign or set mouse 
 //pointer to appropriate place
                              // event.editor.focus();
                              // element.focus();
                          }
                      }
                  };
                  if (attrs.ckeInlineToolbar) {
                      config.toolbar = attrs.ckeInlineToolbar;
                  }

                  var editor = CKEDITOR.inline(a_id, config);
                  if (attrs.ckeInlineOnInit) {
                      uif.apply_scope($rootScope, $parse(attrs.ckeInlineOnInit));
                  }
              }
              else
              {
                  e.focus();
                  element.focus();
              }
          } else
          {
              attrs.$set('contenteditable', false);
          }
    }
    element.click(function () {
      create_and_focus();
    });

    if (attrs.ckeInlineFocusWatch) {
      scope.$watch(function () {
        return $parse(attrs.ckeInlineFocusWatch);
      }, function () {
          if (attrs.ckeInlineFocusCondition == undefined || $parse(attrs.ckeInlineFocusCondition)() == true) {
            create_and_focus();
          }
        })
    }

    var saving = false;


    var save_editor_content = function (cb) {
        if (!saving)
        {
            saving = true;
            var a_id = attrs.ckeInlineId;
            if (a_id) {
                var editor = CKEDITOR.instances[a_id];
              if (editor) {
                var menu_selector = attrs.ckeInlineMenu;
                element.menu.hide();
                  //attrs.$set('contenteditable', false);
                var newValue = editor.getData().replace(/ /, ' ');
                if (ngModel.$modelValue != newValue) {
                    if (attrs.ckeInlineBlurBool) {
                        $parse(attrs.ckeInlineBlurBool).assign($rootScope, false);
                    }
                    ngModel.$setViewValue(newValue);
                    if (attrs.ckeInlineSave) {
                        uif.apply_scope(scope, $parse(attrs.ckeInlineSave));
                    }

                        $timeout(function () {
                            saving = false;
                            if (cb) cb();
                        }, 1100)
                } else
                {
                    saving = false;
                    $rootScope.setup.blcok_edits = false;
                    $rootScope.setup.block_all_editors = false;
                    if (cb) cb();
                }
              }
            }
            else
            {
                saving = false;
            }
        }
    };
  } else if (attrs.ckeNonEditMode) {
    attrs.$set('contenteditable', true);
    element.blur(function () {
      ngModel.$setViewValue(element.html().replace(/ /, ' '));
      if (attrs.ckeInlineSave) {
        uif.apply_scope(scope, $parse(attrs.ckeInlineSave));
      }
    })
  }
}
}
});

0 个答案:

没有答案