富文本ZSSRichTextEditor是iOS原生與網(wǎng)頁交互的集大成者铜秆,各種交互。自然問題也是多多,這篇文文章陸續(xù)更新遇到的奇葩問題。
1.問題1:從頭條這種文章里頭復(fù)制粘貼的文章蜗侈,里邊有圖片篷牌,我們需求并不需要睡蟋,如何過濾?
干了客戶端枷颊,一開始額思路戳杀,總想從客戶端的webview里頭找出路,忙活半天夭苗,并未發(fā)現(xiàn)可以下手的地方信卡,最后只能從網(wǎng)頁這邊想辦法。
最后確定如下思路:
找到html中zss_editor_content這個div容器的粘貼動作(onpaste)-- 在這個方法中遍歷html內(nèi)容(刪除非我們上傳的圖片)
下面就是為html中的zss_editor_content容器 添加粘貼事件题造,由于不延時的話傍菇,執(zhí)行zss_editor.deleteOutAppImg方法,即遍歷圖片內(nèi)容的時候界赔,粘貼還沒完成丢习,所以延時500ms
<body >
<!-- ZSSRichTextEditor Editable Content -->
<!-- 給這個容器添加了一個粘貼事件,延時500毫秒淮悼,不然執(zhí)行事件的時候咐低,web還沒有內(nèi)容,沒法刪除 -->
<div id="zss_editor_content" onpaste="setTimeout('zss_editor.deleteOutAppImg()', 500);" class="zs_editor_content" contenteditable="true" placeholder="請輸入正文"></div>
</body>
問題來了袜腥,如何知道html的圖片不是我們上傳的圖片见擦??
從圖片本身并無好的方法羹令,只能是正則匹配鲤屡,不符合我們服務(wù)器圖片的刪去,但是這太牽強了福侈,比如某個外來圖片剛好符合酒来,那不是gg了
剛好我們的圖片有刪除功能,自然每個圖片標(biāo)簽在插入的時候癌刽,點擊事件就攜帶了一個我們定義的屬性役首,所以通過判斷html內(nèi)容中img是否攜帶這樣的因子,不帶的就不是我們手動插入的显拜,刪除(當(dāng)然下面也會總結(jié)下衡奥,刪除圖片的方法)
下面是插入圖片,就是圖片攜帶因子的瞬間
//插入圖片讓換行
zss_editor.insertImage = function(url, alt) {
zss_editor.restorerange();
var html = '<img src="'+url+'" alt="'+alt+'" onclick="zss_editor.deleteImg(0,this)"/><div><br/></div>';
zss_editor.insertHTML(html);
zss_editor.focusEditor();
zss_editor.enabledEditingItems();
}
接下來看看刪除的具體方法远荠,使用還是jquery矮固,高端了
//使用jquery刪除不是自己上傳的圖片--感謝金哥的鼎力相助
/*自己的圖片有zss_editor.deleteImg(0,this)事件,外頭的圖片沒有*/
zss_editor.deleteOutAppImg = function() {
$('img').each(function(index, obj){
if($(this).attr('onclick') != 'zss_editor.deleteImg(0,this)'){
$(this).remove();
}
});
}
到這里這個外邊圖片的問題就解決了
2.問題2:如何刪除編輯器中已經(jīng)上傳的圖片
嘗試過,網(wǎng)頁直接彈出一個alertview档址,但是有坑盹兢,網(wǎng)頁控制彈出的alertview,他的title是無法自定義的守伸,一直寫個null之類的東西绎秒,直接棄用
所以最后只能采用,js調(diào)用原生尼摹,原生再次調(diào)用js见芹,處理這個問題
代碼如下;
zss_editor.deleteImg = function(type,obj) {
if(type == 0){
object = obj;
deleteNowImg();//調(diào)用原生方法
}else {
$(object).remove();
}
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
self.editorLoaded = YES;
if (!self.topTitleHTML) {
self.topTitleHTML = @"";
}
if (!self.internalHTML) {
self.internalHTML = @"";
}
[self updateTitleHTML];
[self updateHTML];
if(self.placeholder) {
[self setPlaceholderText];
}
if (self.customCSS) {
[self updateCSS];
}
if (self.shouldShowKeyboard) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//[self focusTitleTextEditor];
});
}
/*
Callback for when text is changed, solution posted by richardortiz84 https://github.com/nnhubbard/ZSSRichTextEditor/issues/5
*/
JSContext *ctx = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
ctx[@"contentUpdateCallback"] = ^(JSValue *msg) {
if (_receiveEditorDidChangeEvents) {
[self editorDidChangeWithText:[self getText] andHTML:[self getHTML]];
}
[self checkForMentionOrHashtagInText:[self getText]];
};
[ctx evaluateScript:@"document.getElementById('zss_editor_content').addEventListener('input', contentUpdateCallback, false);"];
ctx[@"zqk"] = ^(JSValue *msg) {
[[NSNotificationCenter defaultCenter] postNotificationName:RichTextTitleFocus object:nil];
};
[ctx evaluateScript:@"document.getElementById('zss_editor_title').addEventListener('focus', zqk, false);"];
ctx[@"qkz"] = ^(JSValue *msg) {
[[NSNotificationCenter defaultCenter] postNotificationName:RichTextContentFocus object:nil];
};
[ctx evaluateScript:@"document.getElementById('zss_editor_content').addEventListener('focus', qkz, false);"];
MJWeakSelf;
ctx[@"deleteNowImg"] = ^() {//刪除圖片
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf deleteImg];
});
};
}
- (void)deleteImg {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"確認(rèn)刪除圖片?"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"確定",nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
NSString *trigger = [NSString stringWithFormat:@"zss_editor.deleteImg(\"%@\", \"%@\");", @"1", @""];
[self.editorView stringByEvaluatingJavaScriptFromString:trigger];//原生通過js調(diào)用刪除方法
}
}
兩端交互,實現(xiàn)圖片的刪除功能