Guest User

Untitled

a guest
Feb 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.18 KB | None | 0 0
  1. @implementation CPTextView : CPView
  2. {
  3. DOMElement FIXME_textArea;
  4.  
  5. id _delegate;
  6.  
  7. CPScrollView _scrollView;
  8. CPView _contentView;
  9.  
  10. JSObject _existingSelectStart;
  11.  
  12. BOOL _alreadyFired;
  13. }
  14.  
  15. -(id)initWithFrame:(CGRect)aFrame
  16. {
  17. self = [super initWithFrame: aFrame];
  18.  
  19. FIXME_textArea = document.createElement("textarea");
  20.  
  21. FIXME_textArea.style.width = "100%";
  22. FIXME_textArea.style.height = "100%";
  23.  
  24. if(document.selection)
  25. FIXME_textArea.style.overflow = "auto";
  26. else
  27. FIXME_textArea.style.overflow = "hidden";
  28.  
  29. FIXME_textArea.style.position = "absolute";
  30.  
  31. FIXME_textArea.style.left = "0";
  32. FIXME_textArea.style.top = "0";
  33. FIXME_textArea.style.border = "0";
  34. FIXME_textArea.style.margin = "0";
  35. FIXME_textArea.style.padding = "0";
  36.  
  37. FIXME_textArea.style.backgroundColor = "rgb(245, 245, 245)";
  38. FIXME_textArea.style.fontSize = "14px";
  39. FIXME_textArea.style.fontFamily = "Arial";
  40.  
  41. FIXME_textArea.style.resize = "none";
  42. FIXME_textArea.style.outlineStyle = "none";
  43.  
  44. FIXME_textArea.onkeydown = function() {
  45. [self textDidChange: self];
  46. };
  47.  
  48. FIXME_textArea.onkeypress = function() {
  49. [self textDidChange: self];
  50. };
  51.  
  52. FIXME_textArea.onkeyup = function() {
  53. [self textDidChange: self];
  54. };
  55.  
  56. if(document.attachEvent)
  57. {
  58. FIXME_textArea.onfocus = function() {
  59. _existingSelectStart = document.body.onselectstart;
  60. document.body.onselectstart = function() { };
  61. };
  62.  
  63. FIXME_textArea.onblur = function() {
  64. if(_existingSelectStart)
  65. document.body.onselectstart = _existingSelectStart;
  66. };
  67. }
  68.  
  69. _DOMElement.appendChild(FIXME_textArea);
  70.  
  71. return self;
  72. }
  73.  
  74. - (void)setDelegate:(id)delegate
  75. {
  76. _delegate = delegate;
  77. }
  78.  
  79. - (id)delegate
  80. {
  81. return _delegate;
  82. }
  83.  
  84. - (void)textDidChange:(id)sender
  85. {
  86. [_delegate textViewDidChange: self];
  87.  
  88. if (!_contentView)
  89. return;
  90.  
  91. var bounds = [_contentView bounds];
  92.  
  93. FIXME_textArea.style.height = CGRectGetHeight(bounds) + "px";
  94. FIXME_textArea.style.height = MAX(CGRectGetHeight(bounds), FIXME_textArea.scrollHeight) + "px";
  95.  
  96. [self setFrameSize:CGSizeMake(CGRectGetWidth(bounds), parseInt(FIXME_textArea.style.height, 10))];
  97. [self scrollToCaret];
  98.  
  99. [[CPRunLoop currentRunLoop] performSelectors];
  100. }
  101.  
  102. - (void)scrollToCaret
  103. {
  104. if(![_scrollView verticalScroller] || [[_scrollView verticalScroller] isHidden])
  105. return;
  106.  
  107. if(FIXME_textArea.selectionStart)
  108. {
  109. var start = FIXME_textArea.selectionStart,
  110. end = FIXME_textArea.selectionEnd;
  111.  
  112. var imposter = document.createElement('div'),
  113. referenceSpan = document.createElement('span'),
  114. stringValue = FIXME_textArea.value;
  115.  
  116. imposter.style.overflow = "hidden";
  117. imposter.style.fontSize = "14px";
  118. imposter.style.padding = "0";
  119. imposter.style.margin = "0";
  120. imposter.style.height = FIXME_textArea.style.height;
  121. imposter.style.width = getComputedStyle(FIXME_textArea, "").getPropertyValue('width');
  122. imposter.style.fontFamily = getComputedStyle(FIXME_textArea, "").getPropertyValue('font-family');
  123.  
  124. for(var i=0; i<start; i++)
  125. {
  126. referenceSpan.innerHTML = stringValue.charAt(i).replace("\n", "<br />");
  127. imposter.appendChild(referenceSpan.cloneNode(true));
  128. }
  129.  
  130. while (imposter.childNodes[start - 1] && imposter.childNodes[start - 1].innerHTML == " ")
  131. start--;
  132.  
  133. document.body.appendChild(imposter);
  134.  
  135. var caretOffsetTop = imposter.childNodes[start - 1].offsetTop - imposter.offsetTop,
  136. caretHeight = imposter.childNodes[start-1].offsetHeight;
  137.  
  138. document.body.removeChild(imposter);
  139. }
  140. else if(document.selection)
  141. {
  142. FIXME_textArea.focus();
  143. var range = document.selection.createRange();
  144.  
  145. window.range = range;
  146. if(range.parentElement() != FIXME_textArea)
  147. return;
  148.  
  149. var caretOffsetTop = range.offsetTop + _DOMElement.offsetTop - 18,
  150. caretHeight = 18;
  151. }
  152. else
  153. return;
  154.  
  155. [self scrollRectToVisible:CGRectMake(1, caretOffsetTop, 1, caretHeight)];
  156. }
  157.  
  158. - (CPString)stringValue
  159. {
  160. return FIXME_textArea.value;
  161. }
  162.  
  163. - (void)setStringValue:(CPString)aString
  164. {
  165. if(aString)
  166. FIXME_textArea.value = aString;
  167. else
  168. FIXME_textArea.value = "";
  169.  
  170. [self textDidChange: self];
  171. }
  172.  
  173. - (void)focus
  174. {
  175. window.setTimeout(function(){
  176. FIXME_textArea.focus();
  177. }, 0);
  178. }
  179.  
  180. - (void)viewDidMoveToSuperview
  181. {
  182. _scrollView = [self enclosingScrollView];
  183. _contentView = [_scrollView contentView];
  184. }
  185.  
  186. - (void)setFrameSize:(CGSize)aSize
  187. {
  188. [super setFrameSize:aSize];
  189.  
  190. [self scrollToCaret];
  191. }
  192.  
  193. @end
Add Comment
Please, Sign In to add comment