Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

How do you map changes between a CodeMirror component and an array of objects?

Đang mở
#641 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
25/100
Loại issue
Tài liệu
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
react, typescript

Hướng nghiên cứu

Bắt đầu với ví dụ InputWithFields và logic handleTextChange của nó trong issue; không có tệp nào trong repository hoặc test nào được xác định. Xác định vị trí phù hợp cho một ví dụ tích hợp CodeMirror/RangeSet, sau đó ghi lại cách xử lý trường hợp sử dụng mảng các đối tượng này và những gì các cập nhật thành công sẽ tạo ra.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

I have an array of objects that are represented in a CodeMirror component. I'm trying to work out how to map changes in the CodeMirror to the object. I'm using a RangeSet to hold the underlying object. I was expecting I could use RangeSet.map() to apply the changes from the CodeMirror onto the object RangeSet. I'm not seeing that happen.

Is there an example that can help me understand how to handle this use case in a good way?

type Text = { type: "text"; textValue: string };
type Field = {
  type: "field";
  textValue: string;
  field: {
    fieldId: string;
  };
};
export type TextWithField = (Text | Field)[];


class TextOrFieldRange extends RangeValue {
  type: "text" | "field";
  textOrField: Text | Field;
  theRange;
  constructor(
    from: number,
    to: number,
    type: "text" | "field",
    textOrField: Text | Field
  ) {
    super();
    this.type = type;
    this.textOrField = { ...textOrField };
    this.theRange = this.range(from, to);
  }

  eq(other: RangeValue): boolean {
    return (
      other instanceof TextOrFieldRange &&
      this.type === other.type &&
      this.theRange.from === other.theRange.from &&
      this.theRange.to === other.theRange.to
    );
  }
}

const InputWithFields = ({
  textWithFields,
  setTextWithFields,
}: {
  textWithFields: TextWithField;
  setTextWithFields: (textWithFields: TextWithField) => void;
}) => {
  const rangeBuilder = new RangeSetBuilder<TextOrFieldRange>();
  let combinedText = "";
  const fieldRanges: [number, number][] = [];

  textWithFields.forEach((item) => {
    const start = combinedText.length;
    combinedText += item.textValue;
    const end = combinedText.length;
    rangeBuilder.add(
      start,
      end,
      new TextOrFieldRange(start, combinedText.length, item.type, item)
    );
    item.type == "field" && fieldRanges.push([start, end]);
  });
  const combinedRanges = rangeBuilder.finish();

  const handleTextChange = (value: string, viewUpdate: ViewUpdate) => {
    const newRanges = combinedRanges.map(viewUpdate.changes.desc);
    const iterator = newRanges.iter();
    const newTextWithFields: TextWithField = [];
    while (iterator.value !== null) {
      newTextWithFields.push(iterator.value.textOrField);
      iterator.next();
    }
    setTextWithFields(newTextWithFields);
  };

  const highlightRanges = rangeHighlighter(fieldRanges);

  return (
    <CodeMirror
      value={combinedText}
      onChange={handleTextChange}
      theme="none"
      basicSetup={false}
      editable={true}
      extensions={[...extensions, highlightRanges]}
      maxWidth="100%"
      height="100px"
    />
  );
};
Ngôn ngữ chính
TypeScript
Star
2.3k
Fork
162
Merge trung bình
2 giờ 41 phút
Pull request đã merge (30 ngày)
1

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của uiwjs/react-codemirror

Tất cả issue của uiwjs/react-codemirror

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.